Breaking

Indexer in C#, Assembly and Versioning in c#


           Indexer in C#
  •  Defining an indexer allows us to create a class that acts like virtual arrays.
  •   Indexer allows the classes to be used in a more intuitive manner.
  •  Indexer interduces a new concept through which objects can be created as an array.
  •  Indexers are also known as a smart array in c#.
  •  An Indexer (also known as index property) is a class property that allows us to access a member of a class by using a feature of an array.   



           Syntax:


<modifier><returntype>this[int i]                //this is indexer name.
{
get
{return <var>
}
set
{
Var [i]=value;                                            //value is a keyword.
 }}

//modifiers should be public, private, protected, and internal.
//this is by default name of indexer which can't be changed.  
//[int i] is an argument list which is used for the parameter.
//return type can be any valid data type in c#.
     
  • An Indexer is always named or access with this keyword.
  •  An Indexer is always defined with an argument.

  •  Indexer consists of two accessors get and set (Indexer is implemented by get and set).
  • We can be overload an indexer.
  • Indexer property can be access by the indexes name.

 using system;
class Test
{
private String[] str=new String [10];
public String this [int i]                                //where this is an indexer.
{
get
{
 return str[i];                                                // set and get are accessor    
}
set
{
str [i]=value;                                                // value is a keyword.
}
}
class Solution
{
public static void Main(string[] args)
{
Test t1=new Test();
t1[0]=”red”;
t1[1]=”blue”;
t1[2]=”green”;
for (int j=0; j<3; j++)
{
Console.WriteLine(t1[j]);
}
}
Output:
red
blue
green


Overload Indexer:-

In C#.net indexer can be overloaded. In indexer, we can pass multiple parameters, which can have different data type.

class OverlaodTest
{
private string[]=new strin[10]

public OverloadTesst()
{
for(int i=0; i<10;i++)
{
list[i]="not";
}
}
public String this[int load]
{
get
{
string tp;
if(load>=0&& load<=10-1)
{
tp=list[load];
}
else
{
tp="";
}
return(tp);
}
set
{
if(load>=0&&load<=10-1)
{
list[load]=value;
}
}
}
public int this[string name]
{
get
{
int load=0;
while(load<10)
{
if(list[load]==name)
{
return laod;
}
load ++;
}
return load;
}
}

static void Main(string[] arg)
{
Overloadtest t= new Overloadtest();
t[0]="Black";
t[1]="Blue";
t[2]="Orange";
t[3]="Pink";
t[4]="Yellow";
t[5]="Red";

for(int i=0; i<10; i++)
{
Console.WriteLine(t[i]);
}
Console.Readkey();
}
}
}

Output:-

Black
Blue
Orange
Pink
Yellow
Red
not
not
not
not

 Assembly and Versioning

 The basic unit of a .net requirement is the assembly. An assembly is a collection of files that appear as a single DLL or Exe (executable file). DLL(Dynamic Link Library file) are the collections of classes and methods that are linked into your running program only when they are needed.

Assembly are the .Net unit of reuse versioning, securty and deployment in addition to object code for the application assembly contain resource such as .GIF file and file defination for each class. Basically assemblies contian one or more module.
The specific version of assembly are recorded in Manitest file(.msf). Versioning increase reuseablty of program and new version of assembly is called versioning.