Facebook Page Twitter Page LinkedIn Page
× C# Tutorial C# Advanced C# Resources


C# is an object-oriented programming language. Everything in C# is associated with classes and objects, along with its member fields and methods. C# allows us to create objects. To create an object, first of all, the object should be defined in the program which is done by creating a class. A class is a code template that is used to define a new object type. Within a class, the object's member fields and methods are defined and when an object is created, its member fields and methods are determined by the class in which it is created.

Create Class

To create a class in C#, class keyword is used. It starts with the keyword followed by class name and a pair of curly braces ({ }). All its member fields and methods goes inside the braces:

Syntax

//defining a class
class class_name {
  access_specifier field;
  access_specifier method;
};

access_specifier: It defines the access type of the class member fields and methods. There are six types of access specifier in C#.

  • public: Member fields and methods of the class are accessible from everywhere.
  • protected: Member fields and methods of the class are accessible in the same class or struct and by derived class.
  • private: Member fields and methods of the class are accessible within the same class or struct only.
  • internal: Member fields and methods of the class are accessible within the same assembly only. It is also the default specifier (when no access specifier is specified)
  • protected internal: Member fields and methods of the class are accessible by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • private protected: Member fields and methods of the class are accessible only within its declaring assembly, by code in the same class or in a type that is derived from that class.

Create Object

To create an object, new keyword followed by class name must be assigned to a variable (object name). To access the member of the class, dot (.) operator is used. Please see the syntax below:

Syntax

//creating an object
class_name object_name = new class_name();

//access class member (member fields and methods)
object_name.field
object_name.method(parameters)

Example:

In the below example, a class (new object type) called Circle is created. An object called MyCircle of the class Circle is created in the main method. The object has only one integer field radius which is accessed in the main method using dot (.) operator.

using System;

class Circle {
  //class field
  int radius = 10;

  static void Main(string[] args) {
    Circle MyCircle = new Circle();

    Console.WriteLine(MyCircle.radius);
  }
}

The output of the above code will be:

10

Class Methods

Class method must be defined inside the class definition. It is defined in the same way as a normal method is defined in C#.

Example: Class method defined inside class

In the below example, a class method called area is defined inside the class Circle. A class method is accessed in the same way as a class field, i.e, using dot (.) operator. Here, the area() class method is used to return the area of the circle object.

using System;

class Circle {
  //class field
  int radius = 10;
  //class method
  public double area() {
    return 22/7.0*radius*radius;
  }

  static void Main(string[] args) {
    Circle MyCircle = new Circle();

    Console.WriteLine(MyCircle.area());
  }
}

The output of the above code will be:

314.3