Classes in C#


What is the Class in C#?

The Class is a Data Structure that holds all the variables, Objects, Methods etc. As the C# is Object Oriented Language, the Class is a very important Data Structure in C#. In C# , the program begins with the declaration of the class. Then all the variable declarations , Assigning values to variables , Creating Objects , Methods declarations and Method calls etc.



Definitions of Some Important Concepts of Class in C#.

  • Access Specifiers- (protected/public/private/internal)-Defines how the class can be accessed.
  • Member Variables- The variables of the class to store specific values to be used later.
  • Member Methods- Method or Function with a group of statements which will be executed simultaneously when the method is called.


Syntax

access_specifier class class_name
{
/*member variables declaration and value assignment*/
      access_specifier data_type variable_name1
      access_specifier data_type variable_name2
      .
      .
      .
      access_specifier data_type variable_nameN

      /*member variables declaration*/
      access_specifier return_type method_name1(arguments)
      {

           /*statements*/
      }
      access_specifier return_type method_name2(arguments)
      {

           /*statements*/
      }
}


Example

Below is the simple example describing the class.

using System;
namespace Class_Post
{
    class Program
    {
       static void Main(string[] args)
       {
          int a= 4;
          int b = 3;
          Console.WriteLine("Value of a=" + a);
          Console.WriteLine("Value of b=" + b);
          Console.ReadLine();
       }
    }
}



Another Example

using System;
namespace Class_Post
{
    class Details
    {
       string name = string.Empty;
       public int Id;
       int age;
       int salary;
      public void accept()
      {
       Console.WriteLine("Enter the name of employee");
       name = Console.ReadLine();
       Console.WriteLine("Enter the age of employee");
       age = Convert.ToInt32(Console.ReadLine());
       Console.WriteLine("Enter the salary of employee");
       salary = Convert.ToInt32(Console.ReadLine());
      }
      public void show()
      {
       Console.WriteLine("The name of employee is " +name);
       Console.WriteLine("The age of employee is " + age);
       Console.WriteLine("The salary of employee is " + salary);
       Console.ReadLine();
      }
    }
}


/*Main class*/

using System;
namespace Class_Post
{
    class Program
    {
       static void Main(string[] args)
       {
        Details emp = new Details();
        emp.Id = 21;
        emp.accept();
        emp.show();
       }
    }
}



Explaination

In above program, the class Details contains the variables ID,name,age and salary. Also the Details class contains two public methods accept() and show one of which accepts the name age and salary of employee from the user and another shows the values entered by the user. In the second class Program, we have created object of Details class and using object emp , we have accessed the variable ID of the Details class. The . is used to access the members and methods of the class.

IMP- Note here we have declared ID variable as public , but not other variables. It's because the default access specifier of the variables is internal. That means the variables and Methods of the Details class will not be accessible to Program class in which main() Method is defined. If we want to access the variables, we need to either declared it public or we have to use a method which is declared as public. Here in above program , we have accessed ID variable by making it public and other variable are accessed via public method.
Classes in C# Classes in C# Reviewed by LanguageExpert on January 06, 2018 Rating: 5

No comments