1.

What is an object in C#?

Answer»

An object in C# is a dynamically created instance of the class that is also known as a RUNTIME entity since it is created at runtime. All the members of the class can be accessed using the object.

The definition of the object starts with the class name that is followed by the object name. Then the new operator is used to CREATE the object.

The syntax of the object definition is given as follows:

ClassName ObjectName = new ClassName();

In the above syntax, ClassName is the name of the class followed by the ObjectName which is the name of the object.

A program that demonstrates an object in C# is given as follows:

using System; NAMESPACE DEMO {   class Product   {     private int x;     private int y;     public void value(int val1, int val2)     {         x = val1;         y = val2;     }     public int returnProduct()     {         return x * y;     }    }    class Test    {      static void Main(string[] args)      {         Product obj = new Product();            obj.value(7, 3);         Console.WriteLine("The product of 7 and 3 is: {0}" , obj.returnProduct());      }    } }

The output of the above program is given as follows:

The product of 7 and 3 is: 21


Discussion

No Comment Found