Using Indexer Definitions in C# Interfaces

Interface with Indexer: public interface IStringDef { string this[int index] { get; set; } } Class implementing the interface: class MyClass : IStringDef { private List<string> allStrings = new List<string>();   public string this[int index] { get { return allStrings[index]; } set { allStrings.Insert(index, value); } } }
Continue reading…

Enjoyed this post? Share it!

 
 

Indexing Data Using String Values in C#

public class PersonCollection : IEnumerable { private Dictionary<string, Person> listOfPeople = new Dictionary<string, Person>();   // This indexer returns a person based on a string index. public Person this[string name] { get { return (Person)listOfPeople[name]; } set { listOfPeople[name] = value; } }   public void ClearPeople() { listOfPeople.Clear(); }   public int Count { […]
Continue reading…

Enjoyed this post? Share it!

 
 

Indexer Methods in C#

// Indexers allow you to access items in an array-like fashion. class Program { static void Main(string[] args) {   PersonCollection allPeople = new PersonCollection();   // Add objects with indexer syntax. allPeople[0] = new Person("Homer", "Simpson", 40); allPeople[1] = new Person("Marge", "Simpson", 38); allPeople[2] = new Person("Lisa", "Simpson", 9); allPeople[3] = new Person("Bart", "Simpson", […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using the index operator in a simple array

static void Main(string[] args) { // Loop over incoming command-line arguments // using index operator. for(int i = 0; i < args.Length; i++) Console.WriteLine("Args: {0}", args[i]);   // Declare an array of local integers. int[] myInts = { 10, 9, 100, 432, 9874};   // Use the index operator to access each element. for(int j […]
Continue reading…

Enjoyed this post? Share it!

 
 

yield break in C#

The yield break instruction specifies that the iterator block is to be abandoned prematurely, without having to add more elements. Below is an example of yield break in c#. A return statement in an iterator block is not allowed therefore you must instead use yield break. static IEnumerable <string> Foo (bool breakit) { yield return […]
Continue reading…

Enjoyed this post? Share it!

 
 

What is c# enumerator and enumerable?

An enumerator is a cursor that is read only and moves forward only through a sequence of values. An enumerator is an object that: 1. Implements IEnumerator 2. Has a method called MoveNext for iterating over the sequence, and a property called Current provides for the reading of the current element of the sequence. The […]
Continue reading…

Enjoyed this post? Share it!

 
 

Arraylist properties and methods in C#

ArrayList alphaList = new ArrayList(); alphaList.Add("A"); alphaList.Add("D"); alphaList.Add("W"); Console.WriteLine("Original Capacity"); Console.WriteLine(alphaList.Capacity); Console.WriteLine("Original Values"); foreach (object alpha in alphaList) { Console.WriteLine(alpha); }   alphaList.Insert(alphaList.IndexOf("D") , "C"); alphaList.Insert(alphaList.IndexOf("W") , "J"); Console.WriteLine("New Capacity"); Console.WriteLine(alphaList. Capacity); Console.WriteLine("New Values"); foreach (object alpha in alphaList) { Console.WriteLine(alpha); }
Continue reading…

Enjoyed this post? Share it!

 
 

Foreach loop to iterate through the elements of the array in C#

int[] intArray = { 1, 2, 3, 4, 5 }; Console.WriteLine("Upper Bound"); Console.WriteLine(intArray. GetUpperBound(0) ); Console.WriteLine("Array elements") ; foreach (int item in intArray) { Console.WriteLine(item) ; } Array.Reverse(intArray) ; Console.WriteLine("Array reversed") ; foreach (int item in intArray) { Console.WriteLine(item) ; } Array.Clear(intArray, 2, 2) ; Console.WriteLine("Elements 2 and 3 cleared") ; foreach (int item in […]
Continue reading…

Enjoyed this post? Share it!

 
 

Convert Enum to Array dynamically in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text;   namespace ProgramEmumtoArray { class ProgramEmumtoArray { static void Main(string[] args) { string[] people = Enum.GetNames(typeof(People)); foreach (var person in people) { Console.WriteLine(person); } Console.Read(); }   enum People { Gandhi, Anthony, Obama, Mani }   } } Output:
Continue reading…

Enjoyed this post? Share it!

 
 

Dynamic enumeration of Enum in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text;   namespace w3mentorConsoleApp { class Program { static void Main(string[] args) { foreach (var car in Enum.GetValues(typeof(Cars))) { Console.WriteLine(Enum.GetName(typeof(Cars), car)); } Console.Read(); } enum Cars { Ferrari, Polo, GMC, FordExplorer } } } Output:
Continue reading…

Enjoyed this post? Share it!