Object is a real world entity, object will be existing physically which means it will occupy memory where as Class is a logical unit.
If we want to use class methods or variables we need to create object, there is no other way to use class methods without creating object.
Ex:
class A
{
public int sum(int a,int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
A obj = new A(); // Creating Object of Class A to access public methods of ClassA
int sum = obj.sum(5, 4);
Console.WriteLine(sum);
Console.ReadKey();
}
}
In Class 'Program' used method 'sum' of another Class A by creating object of Class A
If we want to use class methods or variables we need to create object, there is no other way to use class methods without creating object.
Ex:
class A
{
public int sum(int a,int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
A obj = new A(); // Creating Object of Class A to access public methods of ClassA
int sum = obj.sum(5, 4);
Console.WriteLine(sum);
Console.ReadKey();
}
}
In Class 'Program' used method 'sum' of another Class A by creating object of Class A
0 comments:
Post a Comment