Monday, 16 April 2018

DELEGATES

What is Delegate?
delegate  is similar to a function pointer in C or C++. To call the reference method using  the delegate object.The delegates typedefinition same as the Function typedefinition.


PROGRAM:(DelegateEg.cs)

using System;
delegate int NumberChanger(int n);
namespace Training
{
class DelegateEg
{
static int num=10;
public static int AddNum(int  p)
{
num+=p;
return num;
}
public static int MulNum(int q)
{
num*=q;
return num;

}
public static int getNum()

{
return num;
}
public static void Main(string []arg)
{
NumberChanger nc1=new NumberChanger(AddNum);
NumberChanger nc2=new NumberChanger(MulNum);
nc1(25);
Console.WriteLine("Value of Num:{0}",getNum());
nc2(5);
Console.WriteLine("Value of Num:{0}",getNum());

}
}
}

OUTPUT:

D:\csharp>DelegateEg
Value of Num:35

Value of Num:175


PROGRAM EXPLANATION:
        The above program explain how to use the delegates in the c#.The delegate canbe declared outside the class and to create a object to the delegate and call the function using delegate objects.

Monday, 2 April 2018

Sorted List(GENERIC COLLECTIONS)

COLLECTION:
    A collection is a group of multiple objects. You can group any type of object in a collection into a single collection using a System.Object class. For that the .Net Framework provides collection classes, some in the System.Collections namespace and some in the System.Collecctions.Generic namespace.

GENERIC COLLECTION:
       The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide  the better type safety and performance than non-generic strongly typed collections.
SORTED LIST:
   SORTED LIST is a list it contains a keyvaluepairs and key is unique.

PROGRAM:(GenericCollectionEg.cs)

using System;
using System.Collections.Generic;
using System.Linq;
class GenericCollectionEg
{

public static void Main()
{
SortedList<int,String> slist=new SortedList<int,String>(); //Object Initialization
slist.Add(1,"cat");          //adding key value pair in the sorted list
slist.Add(4,"rat");
slist.Add(3,"bat");
for(int i=0;i<slist.Count;i++)      //Access the sortedlist keyvalues using for loop
Console.WriteLine("key:{0}\t,value:{1}",slist.Keys[i],slist.Values[i]);
foreach(KeyValuePair<int,String> kvp in slist) //using foreach loop
{
Console.WriteLine("key:{0},value:{1}",kvp.Key,kvp.Value);
}
var data=slist[3];  //to assigning a  value to the data (local variable) using key
Console.WriteLine(data);

KeyValuePair<int,String> result=slist.Where(g=>g.Key==4).FirstOrDefault(); // using linq
Console.WriteLine(result.Key+""+result.Value);



var query=from kvp in slist
  where kvp.Key==1
  select kvp;
var res=query.FirstOrDefault();
Console.WriteLine("Key:{0},Values:{1}",res.Key,res.Value);


var re=(from kvp in slist
where kvp.Key==3
select kvp).FirstOrDefault();
Console.WriteLine("Key:{0},Values:{1}",re.Key,re.Value);
        }
    }

OUTPUT:

D:\csharp>GenricCollectionEg
key:1   ,value:cat
key:3   ,value:bat
key:4   ,value:rat
key:1,value:cat
key:3,value:bat
key:4,value:rat
bat
4rat
Key:1,Values:cat
Key:3,Values:bat

PROGRAM EXPLANATION:
     The above program explain the sorted list,the sorted similar to the dictionary but it sorted based on the key value automatically.And then the sorted list storing the data in the list .The sortedlist under the genericcolections.