Introduction
My intention for writing this article is to describe an iterIndex Code Snippet
and its usage. But I will explain a bit of Code Snippets to make the article
simpler.
What is Code Snippet
Visual Studio provides a feature called Code Snippets. You can use Code Snippets
to type a short alias, and then expand it into a common programming construct,
i.e. predefined code.
For example, the "for" Code Snippet creates an empty for loop, as in:
- for (int i = 0; i < length; i++)
- { }
And the "propfull" Code Snippet creates a full property, as in:
- private int myVar;
- public int MyProperty
- {
- get { return myVar; }
- set { myVar = value; }
- }
How to use Code Snippets?
To use Code Snippets through keyboard shortcut
- In the Visual Studio IDE, open the file
that you intend to edit.
- In the Code Editor, place the cursor
where you would like to insert the Code Snippet.
- Type CTRL+K, CTRL+X.
- Select the Code Snippet from the Code
Snippet inserter and then press "Tab" or "Enter".
Alternatively, you can type the name of the
Code Snippet, and then press "Tab" or "Enter".
To use Code Snippets through IntelliSense auto-completion
- In the Visual Studio IDE, open the file
that you intend to edit.
- In the Code Editor, place the cursor
where you would like to insert the Code Snippet.
- Type the shortcut for the Code Snippet
that you want to add to your code.
- Type Tab, Tab to invoke the Code Snippet.
What are the benefits?
- Coding quickly
- No tension of remembering syntax.
- An easy approach to follow design/coding
standards
- And the best thing is, we can define our
own Code Snippets and ask the team to follow our project specific coding
standards.
What is iterIndex?
It creates a "named" iterator and indexer pair by using a nested class. It
creates a sub class to support iteration, and also provides an indexer.
Let's create a class Student
- public class Student
- {
- public String Name;
- public int Age;
- }
And now we need a class Students, that will be a collection of Student objects.
So let's define our Students Class:
- class Students
- {
- Student[] students = new Student[100];
- int idx = -1, Tot = -1;
-
- public void Add(Student std)
- {
- students[++Tot] = std;
- }
- public void Add(String name, int age)
- {
- students[++Tot] = new Student()
- {
- Name = name,
- Age = age
- };
- }
- }
Now place the cursor below the commented code //iterIndex will come here. And
type iterindex and press Tab, Tab.
Wow!, we got predefined code generated by the IDE, here is the sample:
- class Students
- {
- Student[] students = new Student[100];
- int idx = -1, Tot = -1;
-
- public MyViewIterator MyView
- {
- get { return new MyViewIterator(this); }
- }
- public class MyViewIterator
- {
- readonly Students outer;
- internal MyViewIterator(Students outer)
- {
- this.outer = outer;
- }
-
- public int Length { get { return 1; } }
- public ElementType this[int index]
- {
- get
- {
-
-
-
-
-
- throw new NotImplementedException();
- return default(ElementType);
- }
- }
- public System.Collections.Generic.IEnumerator GetEnumerator()
- {
- for (int i = 0; i < this.Length; i++)
- {
- yield return this[i];
- }
- }
- }
- public void Add(Student std)
- {
- students[++Tot] = std;
- }
- public void Add(String name, int age)
- {
- students[++Tot] = new Student()
- {
- Name = name,
- Age = age
- };
- }
- }
Points to Notice
- MyViewIterator Class
Sinces it is a sub class, it will have full access to all it members, i.e
private members too.
- MyViewIterator Constructor
It accepts a Students Object, and assigns it to the outer property.
- Indexer inside MyViewIterator
It also provides an indexer to access a collection item through the index.
- The GetEnumerator method
We have implemented neither IEnumerator nor IEnumerable, this will help us
to iterate through the collection of Students.
- And MyView property inside Students
class
It returns a new object of MyViewIterator class.
Now let's change the snippet code as in the
following
- public class Students
- {
- Student[] students = new Student[100];
- private int Tot = 0;
- public void Add(Student std)
- {
- students[Tot++] = std;
- }
- public void Add(String name, int age)
- {
- students[Tot++] = new Student()
- {
- Name = name,
- Age = age
- };
- }
- public MyViewIterator MyView
- {
- get { return new MyViewIterator(this); }
- }
- public class MyViewIterator
- {
- readonly Students outer;
- internal MyViewIterator(Students outer)
- {
- this.outer = outer;
- }
-
- public int Length { get { return outer.Tot; } }
- public Student this[int index]
- {
- get { return outer.students[index]; }
- }
- public System.Collections.Generic.IEnumerator GetEnumerator()
- {
- for (int i = 0; i < this.Length; i++)
- {
- yield return outer.students[i];
- }
- }
- }
- }
Let's add some Students to our Students class
- class Program
- {
- static void Main()
- {
- Students s = new Students();
- s.Add("Ajay", 27);
- s.Add("Raj", 28);
- }
- }
Using MyViewIterator.GetEnumerator to iterate through each Student
- Students.MyViewIterator iterator = s.MyView;
- {
- using (var student = iterator.GetEnumerator())
- {
- while (student.MoveNext())
- {
- Console.WriteLine(student.Current.Name);
- }
- }
- }
Using foreach to iterate through each Student
- foreach (Student std in s.MyView)
- {
- Console.WriteLine(std.Name);
- }
Using Indexer to get the student:
- Student StdAjay = s.MyView[0];
- Console.WriteLine(StdAjay.Name + " " + StdAjay.Age.ToString());
Complete Code block
- class Program
- {
- static void Main()
- {
- Students s = new Students();
- s.Add("Ajay", 27);
- s.Add("Raj", 28);
- Students.MyViewIterator iterator = s.MyView;
- {
- using (var student = iterator.GetEnumerator())
- {
- while (student.MoveNext())
- {
- Console.WriteLine(student.Current.Name);
- }
- }
- }
- foreach (Student std in s.MyView)
- {
- Console.WriteLine(std.Name);
- }
- Student StdAjay = s.MyView[0];
- Console.WriteLine(StdAjay.Name + " " + StdAjay.Age.ToString());
- }
- }
So do start using Code Snippets and do coding in a smarter way.