Indexer Implemented C# with Example



Indexer Implemented C# with Example

 	using System;

	namespace CSharpClass
	{
		class Employee
		{
			public string LastName; // Call this field 0.
			public string FirstName; // Call this field 1.
			public string CityOfBirth; // Call this field 2.
			public string this[int index] // Indexer declaration
			{
				set // Set accessor declaration
				{
					switch (index) 
					{
						case 0: 
							LastName = value;
						break;
						
						case 1: 
							FirstName = value;
						break;
						
						case 2: 
							CityOfBirth = value;
						break;
						
						default:
							throw new ArgumentOutOfRangeException("index");
					}
				}

				get // Get accessor declaration
				{
					switch (index) 
					{
						case 0: 
							return LastName;
							
						case 1: 
							return FirstName;
							
						case 2: 
							return CityOfBirth;
							
						default: 
							throw new ArgumentOutOfRangeException("index");
					}
				}
			}
		}
		
		class UsingEmployee
		{
			static void Main(String[] arg)
			{
				Employee emp1 = new Employee();
				emp1[0] = "Doe";
				emp1[1] = "Jane";
				emp1[2] = "Dallas";

				Console.WriteLine("Last Name: " + emp1[0]);
				Console.WriteLine("First Name: " + emp1[1]);
				Console.WriteLine("City of Birth: " + emp1[2]);
			}
		}	
	}
 

0 Comment's

Comment Form

Submit Comment