Yes,we have many Classes inside Instance class which can be created instance.as well as Static methods are accessable through the class name only.
Static method: a method performed the action,the end result is same for if we create an instance to the class otherwise also. so creating instance for such methods are wastage of memory. so we are accessing them with the class name.
class StaticMethodDemo
{
private int Add(int x, int y)
{
return x + y;
}
static void Main()
{
StaticMethodDemo obj = new StaticMethodDemo();
int result=obj.Add(10, 20);
Console.WriteLine(result);
Console.ReadKey();
}
}
but instead of creating object is meaningless to perform this task.and additional burdan to garbage collector to maintain generations and destroing is very lenght process.
so make it as static
class StaticMethodDemo
{
private static int Add(int x, int y)
{
return x + y;
}
static void Main()
{
int result = StaticMethodDemo.Add(10, 20);
Console.WriteLine(result);
Console.ReadKey();
}
}
this is a general example.Now we will see how .net framework built like these funcationalities.
Ex: we will discuss Array Class
Array Class contains static methods as well as instance methods also.
1. CopyTo() is instance method.
2. Copy() is static method.
Static method: a method performed the action,the end result is same for if we create an instance to the class otherwise also. so creating instance for such methods are wastage of memory. so we are accessing them with the class name.
class StaticMethodDemo
{
private int Add(int x, int y)
{
return x + y;
}
static void Main()
{
StaticMethodDemo obj = new StaticMethodDemo();
int result=obj.Add(10, 20);
Console.WriteLine(result);
Console.ReadKey();
}
}
but instead of creating object is meaningless to perform this task.and additional burdan to garbage collector to maintain generations and destroing is very lenght process.
so make it as static
class StaticMethodDemo
{
private static int Add(int x, int y)
{
return x + y;
}
static void Main()
{
int result = StaticMethodDemo.Add(10, 20);
Console.WriteLine(result);
Console.ReadKey();
}
}
this is a general example.Now we will see how .net framework built like these funcationalities.
Ex: we will discuss Array Class
Array Class contains static methods as well as instance methods also.
1. CopyTo() is instance method.
2. Copy() is static method.
Why CopyTo() method declared as instance method?
CopyTo method is used to copy all the element in the array.[source array]
EX:
int[] arrint1 = new int[4];
arrint1[0] = 10;
arrint1[1] = 20;
arrint1[2] = 30;
arrint1[3] = 40;
int[] arrint2 = new int[10];
arrint1.CopyTo(arrint2, 4);
foreach (int item in arrint2)
{
Console.WriteLine(item);
}
Note: destination array size should be maximum then source why because copyto copies all the elements.
The the fixablity is which from index to which index based we can copy all the elements to destination
but require space to copy all the elements.
why copy method is declared as Static method?
Using copy method also I can copy all the elements [or] range of element from source to destination array.
Array.Copy(arrint1,arrint2,length);
Note:Length should be not greater then source arry length.
Using copy method also I can copy all the element to destination array also.
int[] arrint3 = new int[arrint1.Length];
arrint1.Length—>again full array.
Array.Copy(arrint1, arrint3, arrint1.Length);
// all elements coping again to arrint3 array.
foreach (int item in arrint3)
{
Console.WriteLine(item);
}
so
what is the use of copy method and why it is delcared as static method?
Destination array size is our choice.how much size is required is programmer choice.instead of created of full array is again vastage of memory for second array.
EX:
int[] arrint3 = new int[2];
Array.Copy(arrint1, arrint3,2);
foreach (int item in arrint3)
{
Console.WriteLine(item);
}
The first 2 elements from the source array to destination array will be copied.not possible to store the elements form soruce to destination based on indexing like copyTo method.
No comments:
Post a Comment