top
upGrad KnowledgeHut SkillFest Sale!

Search

C# Tutorial

A class is a data structure in C# that combines data variables and functions into a single unit. Instances of the class are known as objects. While a class is just a blueprint, the object is an actual instantiation of the class and contains data. The different operations are performed on the object.Class DefinitionA new class requires a class definition. It starts with the class keyword and contains the class name. It also includes the attributes and modifiers of the class as well as its interfaces. Then the class body is provided within curly braces.The syntax of a class definition is given as follows:AccessSpecifier class NameOfClass {     // Member variables     // Member functions }In the above syntax, AccessSpecifier provides the access specifier for the class. Then the keyword class is used which is followed by the NameOfClass. Then the class body is provided.Object DefinitionAn object is a dynamically created instance of the class. It is created at runtime so it can also be called a runtime entity. All the members of the class can be accessed using the object.The object definition starts with the class name followed by the object name. Then the new operator is used to create the object.The syntax of the object definition is given as followsNameOfClass NameOfObject = new NameOfClass();In the above syntax, NameOfClass is the class name followed by the NameOfObject which is the object name.A program that demonstrates class definition and object definition is given as follows:Source Code: Program that demonstrates class definition and object definition in C#using System; namespace ClassDemo {   class Sum   {     private int x;     private int y;     public void setVal(int val1, int val2)     {         x = val1;         y = val2;     }     public int getSum()     {         return x + y;     }    }    class Test    {      static void Main(string[] args)      {         Sum s = new Sum();            s.setVal(3,14);         Console.WriteLine("Sum of 3 and 14 is: {0}" , s.getSum());      }    } }The output of the above program is given as follows:Sum of 3 and 14 is: 17Static Variables in ClassStatic variables in a class have only one instance even if there are multiple objects of the class. Because of this reason, static variables can be used to define constants. Static variables can be initialized inside the class definition or outside it using the static keyword.A program that demonstrates static variables is given as follows:Source Code: Program that demonstrates static variables in C#using System; namespace StaticVariableDemo {   class StaticVariable   {      public static int count;      public StaticVariable()      {         count++;      }      public int getCount()      {         return count;      }   }   class Test   {      static void Main(string[] args)      {         StaticVariable s1 = new StaticVariable();         StaticVariable s2 = new StaticVariable();         StaticVariable s3 = new StaticVariable();         Console.WriteLine("Value of count for s1: {0}", s1.getCount());         Console.WriteLine("Value of count for s2: {0}", s2.getCount());         Console.WriteLine("Value of count for s3: {0}", s3.getCount());      }   } }The output of the above program is as follows:Value of count for s1: 3 Value of count for s2: 3 Value of count for s3: 3Static Function in ClassA member function in a class can also be declared as static using the static keyword. This function can access only static variables. It can also exist before any object of the class is created.A program that demonstrates static function in class is given as follows:Source Code: Program that demonstrates static function in C#using System; namespace StaticFunctionDemo {   class StaticDemo   {      public static int count = 5;      public StaticDemo()      {         Console.WriteLine("Static Demo");      }      public static int getCount()      {         count++;         return count;      }   }   class Test   {      static void Main(string[] args)      {         Console.WriteLine("Value of count: {0}", StaticDemo.getCount());              }   } }The output of the above program is as follows:Value of count: 6
logo

C# Tutorial

Classes and Objects in C#

A class is a data structure in C# that combines data variables and functions into a single unit. Instances of the class are known as objects. While a class is just a blueprint, the object is an actual instantiation of the class and contains data. The different operations are performed on the object.

Class Definition

A new class requires a class definition. It starts with the class keyword and contains the class name. It also includes the attributes and modifiers of the class as well as its interfaces. Then the class body is provided within curly braces.

The syntax of a class definition is given as follows:

AccessSpecifier class NameOfClass
{
    // Member variables
    // Member functions
}

In the above syntax, AccessSpecifier provides the access specifier for the class. Then the keyword class is used which is followed by the NameOfClass. Then the class body is provided.

Object Definition

An object is a dynamically created instance of the class. It is created at runtime so it can also be called a runtime entity. All the members of the class can be accessed using the object.

The object definition starts with the class name 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

NameOfClass NameOfObject = new NameOfClass();

In the above syntax, NameOfClass is the class name followed by the NameOfObject which is the object name.

A program that demonstrates class definition and object definition is given as follows:

Source Code: Program that demonstrates class definition and object definition in C#

using System;
namespace ClassDemo
{
  class Sum
  {
    private int x;
    private int y;
    public void setVal(int val1, int val2)
    {
        x = val1;
        y = val2;
    }
    public int getSum()
    {
        return x + y;
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Sum s = new Sum();   
        s.setVal(3,14);
        Console.WriteLine("Sum of 3 and 14 is: {0}" , s.getSum());
     }
   }
}

The output of the above program is given as follows:

Sum of 3 and 14 is: 17

Static Variables in Class

Static variables in a class have only one instance even if there are multiple objects of the class. Because of this reason, static variables can be used to define constants. Static variables can be initialized inside the class definition or outside it using the static keyword.

A program that demonstrates static variables is given as follows:

Source Code: Program that demonstrates static variables in C#

using System;
namespace StaticVariableDemo
{
  class StaticVariable
  {
     public static int count;
     public StaticVariable()
     {
        count++;
     }
     public int getCount()
     {
        return count;
     }
  }
  class Test
  {
     static void Main(string[] args)
     {
        StaticVariable s1 = new StaticVariable();
        StaticVariable s2 = new StaticVariable();
        StaticVariable s3 = new StaticVariable();
        Console.WriteLine("Value of count for s1: {0}", s1.getCount());
        Console.WriteLine("Value of count for s2: {0}", s2.getCount());
        Console.WriteLine("Value of count for s3: {0}", s3.getCount());
     }
  }
}

The output of the above program is as follows:

Value of count for s1: 3
Value of count for s2: 3
Value of count for s3: 3

Static Function in Class

A member function in a class can also be declared as static using the static keyword. This function can access only static variables. It can also exist before any object of the class is created.

A program that demonstrates static function in class is given as follows:

Source Code: Program that demonstrates static function in C#

using System;
namespace StaticFunctionDemo
{
  class StaticDemo
  {
     public static int count = 5;
     public StaticDemo()
     {
        Console.WriteLine("Static Demo");
     }
     public static int getCount()
     {
        count++;
        return count;
     }
  }
  class Test
  {
     static void Main(string[] args)
     {
        Console.WriteLine("Value of count: {0}", StaticDemo.getCount());        
     }
  }
}

The output of the above program is as follows:

Value of count: 6

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

austin faith

Avery good write-up. Please let me know what are the types of C# libraries used for AI development.

kariya arti

very satisfied!!

jean-Francois Michaud

Good tutorial. Small question: Say, there is : enum numbers { one, two, three} and a string field_enum ="one" how would I from the variable field_enum have a response with value numbers.one so that it can be treated as an enum and not as a string. making a list from the enum, and loop into the list. is not elegant... and may not work is forced value on field is forced ( one = 100).

Kshitiz

Hi Team Knowledge Hut, Thank you for such an informative post like this. I am completely new to this digital marketing field and do not have much idea about this, but your post has become a supportive pillar for me. After reading the blog I would expect to read more about the topic. I wish to get connected with you always to have updates on these sorts of ideas. Regards, Kshitiz

Ed

The reason abstraction can be used with this example is because, the triangle, circle. Square etc can be defined as a shape, for example.....shape c = new circle(5,0)...the abstract object c now points at the circle class. Thus hiding implementation

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial