Extension Methods in C#

Extension Methods

There are some function that can be done on a string like String.Split(‘ ‘) ;

To add a new method like that we can go for using a Extension method.

In the following example I have added a new function test.getLength_Ext() to a string.

To do it

1) Create an accessible class with an accessible method

2) Both must be static If not it does not make any difference from creating a class.

3) The parameter(first) must be this followed by the type it must extend. In this case it Extends a string so I use test.getLength_Ext(this String)

public static class a

{
public static int getLength_Ext(this string str)
{
return str.Length;
}
}
class Program
{
static void Main(string[] args)
{
string test = “ABCDE”;
Console.WriteLine(test.getLength_Ext());
Console.ReadLine();
}
}

Hope you Understood . 🙂

Use of Global in C#

Definition From MSDN

The global contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed.

 

static void Main(string[] args)
{

// Define a Console integer to create a conflict

const int Console = 1;

// Console.WriteLine(“Test”); will not work as Console is a integer in the scope

// global in the following line says it has to refer to the Global namespace and not the current namespace.
global::System.Console.WriteLine(“Test”);
global::System.Console.ReadLine();
}

Hope You Understood 🙂 .

Difference Between Struct and Classes in C#

The main difference between class and Struct

Class – Reference type .

Which means only a reference to a location is stored.

Struct – Value type.

Which means the value is stored.

Now lets create a class and struct a simple one that looks like.

// A class song

class Song
{
public string name;

}

// A struct songs

struct Songs

{
public string name;
}

// A console Application to demonstrate
class Program
{
static void Main(string[] args)
{
// reference type  – Two classes are created s , s1 
Song s = new Song();
Song s1 = new Song();

// A value is assigned to s 


s.name = “All Rise”;

// s –> s1 which means the reference to the value at a memory location is assigned to s1 


s1 = s;
Console.WriteLine(“Initial Value”);
Console.WriteLine(“from s1 :” + s1.name);
Console.WriteLine(“from s :” + s.name);

Console.WriteLine(“change to s1”);

//Any change to s1 is reflected to s also 


s1.name = “Beautiful”;
Console.WriteLine(“from s1 :”+s1.name);
Console.WriteLine(“from s :” + s.name);
Console.ReadLine();

// value type
Songs s3 = new Songs();
Songs s4 = new Songs();
s3.name = “All Rise”;
s4 = s3;
Console.WriteLine(“Initial Value”);
Console.WriteLine(“from s1 :” + s4.name);
Console.WriteLine(“from s :” + s3.name);

 

// Same thing is done in a struct but the values in s3 and s4will be different 

 

Console.WriteLine(“change to s1”);
s4.name = “Beautiful”;
Console.WriteLine(“from s1 :” + s4.name);
Console.WriteLine(“from s :” + s3.name);
Console.ReadLine();
}
}

Output

Initial Value
from s1 :All Rise
from s :All Rise
change to s1
from s1 :Beautiful
from s :Beautiful

Initial Value
from s1 :All Rise
from s :All Rise
change to s1
from s1 :Beautiful
from s :All Rise

Hope you got the Difference. Ideally you can use a struct in case the size of the object is small. And you have a case as mentioned above.

How ever there are some things you need to know before using a struct .

– cannot have explicit parameterless constructor where as a class can.
-cannot have destructors.
-can’t inherit from another class.

 

Hope You got the Difference 🙂 .