public static void Examples() { // <SnippetExampleCode> IndexerClass test = new IndexerClass(); System.Random rand = new System.Random(); // Call the indexer to initialize its elements. for (int i = 0; i < 10; i++) { test[i] = rand.Next(); } for (int i = 0; i < 10; i++) { System.Console.WriteLine($"Element #{i} = {test[i]}"); } /* Sample output: * Element #0 = 360877544 * Element #1 = 327058047 * Element #2 = 1913480832 * Element #3 = 1519039937 * Element #4 = 601472233 * Element #5 = 323352310 * Element #6 = 1422639981 * Element #7 = 1797892494 * Element #8 = 875761049 * Element #9 = 393083859 */ // </SnippetExampleCode> }
public void Indexer() { var indexer = new IndexerClass(); indexer[0] = "foo"; AssertEquals(indexer[0], "foo"); }
public static void InterfaceIndexersMain() { IIndexers iIndexer = new IndexerClass(); Console.WriteLine(iIndexer[0]); Console.WriteLine(iIndexer[1]); Console.WriteLine(iIndexer[2]); Console.WriteLine(iIndexer[3]); Console.ReadLine(); }
public static void Main() { IndexerClass b = new IndexerClass(); // call the indexer to initialize the elements #3 and #5: b[3] = 256; b[5] = 1024; for (int i=0; i<=10; i++) { Console.WriteLine("Element #{0} = {1}", i, b[i]); } }
public static void Main() { IndexerClass b = new IndexerClass(); // call the indexer to initialize the elements #3 and #5: b[3] = 256; b[5] = 1024; for (int i = 0; i <= 10; i++) { Console.WriteLine("Element #{0} = {1}", i, b[i]); } }
public void Indexer_Properties_Are_Ignored() { var containerManager = new ContainerManager(); var testObject = new IndexerClass(); containerManager.AddObjectToContainer("container", testObject, "obj"); var reference = containerManager.GetInstancesInContainer("container") .Single(x => x.Name == "obj"); var details = containerManager.GetInstanceDetails(reference.InstanceId); details.Properties.Length.Should().Be(0); }
static void Main() { IndexerClass test = new IndexerClass(); System.Random rand = new System.Random(); // Call the indexer to initialize its elements. for (int i = 0; i < 10; i++) { test[i] = rand.Next(); } for (int i = 0; i < 10; i++) { System.Console.WriteLine("Element #{0} = {1}", i, test[i]); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); }
static void Main() { IndexerClass test = new IndexerClass(); System.Random rand = new System.Random(); // Call the indexer to initialize its elements. for (int i = 0; i < 10; i++) { test[i] = rand.Next(); } for (int i = 0; i < 10; i++) { System.Console.WriteLine("Element #{0} = {1}", i, test[i]); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); }
public static void Execute() { IndexerClass Team = new IndexerClass(); Team[0] = "Rocky"; Team[1] = "Teena"; Team[2] = "Ana"; Team[3] = "Victoria"; Team[4] = "Yani"; Team[5] = "Mary"; Team[6] = "Gomes"; Team[7] = "Arnold"; Team[8] = "Mike"; Team[9] = "Peter"; for (int i = 0; i < 10; i++) { Console.WriteLine(Team[i]); } Console.WriteLine("Index positon of Gomes : " + Team["Gomes"]); }
static void Main() { IndexerClass indexer = new IndexerClass(); indexer[1] = 5; indexer[2] = 2; indexer[3] = 13; indexer[4] = 20; indexer[-2] = -4; // Not in the boundries of the array int val1 = indexer[1]; int val2 = indexer[2]; int val3 = indexer[3]; int val4 = indexer[4]; int val5 = indexer[-2]; // Not in the boundries of the array Console.WriteLine(val1); Console.WriteLine(val2); Console.WriteLine(val3); Console.WriteLine(val4); Console.WriteLine(val5); // Not in the boundries of the array }
static void Main(string[] args) { #region Interface - Property caller Console.WriteLine(); // Implementing property : Setting value to the property in IF extended class MyClass objMC = new MyClass(); objMC.Name = "shiva"; Console.WriteLine("Name provided (Interface) : " + objMC.Name); Console.WriteLine(); IFTest objT = new IFTest(); // Calling SaveData would be ambiguous (since both Interface has same method name). Compiler calls both implementation string strIsIdatastore = (objT is IDataStore) ? "Yes" : "No"; string strIsISerializabl = (objT is IDataStore) ? "Yes" : "No"; objT.SaveData(); Console.WriteLine("Is IDataStore called : " + strIsIdatastore); Console.WriteLine("Is ISerializabl called : " + strIsISerializabl); Console.WriteLine(); // To call a specific member of the interface casting can be done accordingly ((ISerializabl)objT).SaveData(); ((IDataStore)objT).SaveData(); Console.WriteLine(); EditBox edit = new EditBox(); edit.SaveData(); Console.WriteLine(); #endregion #region Abstract class - sum caller // Abstract class cannot be instantiated AddClass objAC = new AddClass(); int sum = objAC.Add(2, 3); Console.WriteLine("Sum of two nos (using Abstract Class) : " + sum); Console.WriteLine(); #endregion #region Polymorphism - callign base class/derived class methods // Polymorphism : Calling base class method BaseClassPoly objBCP = new BaseClassPoly(); string strWelcomeBase = objBCP.Welcome(); Console.WriteLine(strWelcomeBase); Console.WriteLine(); // calling derived class & methods DerivedClassPoly objDCP = new DerivedClassPoly(); string strWel = objDCP.Welcome("shiva"); Console.WriteLine(strWel); Console.WriteLine(objDCP.DateDetails()); Console.WriteLine(objDCP.Welcome()); Console.WriteLine(); #endregion #region Indexer - set & get for int & string indexers // Indexer. setting and getting values IndexerClass objIC = new IndexerClass(); objIC[1] = 1; objIC[0] = 0; objIC[2] = 2; // This value cannot be assigned as indexer size is only 3 (zero based) // Inside indexer setter : Value would be assignd as Zero as the index is outside bounds of the array objIC[4] = 10; for (int i = 0; i <= 5; i++) { Console.WriteLine("Indexer value for objIC[" + i + "] : " + objIC[i]); } // Indexer can be overloaded. multiple indexers can co-exist in a class // below indexer returns String output for the day provided Console.WriteLine(); string dayCount = objIC["FRI"]; Console.WriteLine("Indexer day count for Friday : " + dayCount); Console.WriteLine(); #endregion #region Delegates - sum/sub & multicast // delegates are references to method // Create an instance of the class & pass the method as reference to delegate object DelegateClass objDC = new DelegateClass(); // Method can be passed as reference to delegate in below way also or can be assigned directly. // AddSubDelegate objAddSubDel = new AddSubDelegate(objDC.Add); AddSubDelegate objAddSubDel = objDC.Add; int delsum = objAddSubDel(1, 2); Console.WriteLine("Delegate := Sum & sub of 1 & 2 : " + delsum); // Multicast delegates Console.WriteLine(); objDC = new DelegateClass(); AddSubDelegate objAddSubMultiDel = null; objAddSubMultiDel = new AddSubDelegate(objDC.Add); objAddSubMultiDel += new AddSubDelegate(objDC.Sub); int delSumSub = objAddSubMultiDel(1, 2); Console.WriteLine("Multicast Delgate : Add & sub of 1,2 methods called: " + delSumSub); //removing sub method objAddSubMultiDel -= new AddSubDelegate(objDC.Sub); Console.WriteLine("Multicast Delgate : removing sub of 1,2 method : " + objAddSubMultiDel(1, 2)); Console.WriteLine(); #endregion #region Events // call event class & assign ChangeEvent with the corresponding handler EventClass objEC = new EventClass(); objEC.ChangeEvent += new MyDelegate(objEC.ChangeEventMethod); // ChangeEvent is called everytime when the setter is called in EventClass objEC.Price = 10; objEC.Price = 20; Console.WriteLine(); #endregion #region Value & Reference ValueAndRefClass objVAR = new ValueAndRefClass(); // Implicit Value types (like int) are always passed by value int a; objVAR.DoWork(out a); Console.WriteLine("Value of A is :" + a); Console.WriteLine(); int b = 3; Console.WriteLine("Value of B before REF is :" + b); objVAR.DoWorkB(ref b); Console.WriteLine("Value of B after REF is :" + b); Console.WriteLine(); // Object Types are impicitly passed by reference int[] nums = { 1, 3, 5 }; objVAR.DoWorkObject(nums); int count = 0; foreach (int n in nums) { Console.WriteLine("Value of a[{0}] is {1}", count++, n); } Console.WriteLine(); #endregion }
public void Indexer() { var indexer = new IndexerClass(); indexer[0] = "foo"; AssertEquals(indexer[0], "foo"); }