Skip to main content

Class (C#)

Class is a template, declaration or blueprint that is used for classifying object. It encapsulates variable members, functions, structure, properties and many more components. It is the basic building block of object oriented programming. In order to create class, the class keyword is used.


Syntax:


 class print
  {

  }






After creating class, you can use it by creating its object. An object is created using new keyword. Suppose you created a class print that contain following method.



 class print
  {
    public void printname()
     {
       Console.WriteLine("My name is Steven Clark");
     }
  }


To use the members of class, you need to create object of this class. After creating the object of print class, you can use its members using the object name as follow:


 print pr = new print();
 pr.printname();





 Programming Example of creating class in C#


using System;

namespace Creating_Class
{
  class accept //Creating 1st. class
   {
     public string name;
     public void acceptdetails()
      {
        Console.Write("Enter your name:\t");
        name = Console.ReadLine();
      }
   }

  class print // Creating 2nd class
   {
     public void printdetails()
      {
        //Creating object of 1st. class
        accept a = new accept();
        //executing method of 1st class.
        a.acceptdetails();
        //Printing value of name variable
        Console.WriteLine("Your name is " + a.name);
      }
   }
  class Program //Creating 3rd class
   {
     static void Main(string[] args)
      {
        print p = new print();
        p.printdetails();
        Console.ReadLine();
      }
   }
}



Output:




Guideline while creating class

1. The class name should be noun and meaningful.

2. Use either pascal case or camel case. In camel case, the first letter is small. Ex. camelCase. In pascal case first letter is capital. Ex. PascalCase. It is strictly recommended you to use pascal case for class name and camel case for variable name.

3. Your class name should not contain any special character except underscore (_) or digit. Must start your class name with character.

4. Don’t use reserved keyword for class name.







Comments

Popular posts from this blog

C# Array

Array is a collection of variable of same data type. If you have declare 1000 integer variable, then you can declare an integer type array of 1000 size. The value of array can be accessed using index position of array. The first index position of array is zero. In C#, there two types of array: Single Dimensional Array and Multi Dimensional Array. You can use both type of array easily and can access its element using loop constructs or index position.

Structure (C#)

Structure is the value type data type that can contain variables, methods, properties, events and so on. It simplifies the program and enhance performance of code in C# programming. The structure encapsulate small group of related variables inside a single user-defined data type. It improves speed and memory usage and also enhances performance and clarity of your code. How to use structure in C#? It is very simple to use structure in C#. The following programming example will show you to how to create and use structure in C# programming. Programming Example of Structure (C#) using System; namespace Structure {    class Program    {      // creating three different variable in single structure       struct book        {          public string bookname;          public int price;          public stri...