C# Class

C# Class plays an important role. Through classes, we can associate everything with C#. A Class in C# is known as an object constructor. To solve problems in a better way we always divide the problem into objects.

A class is similar to a blueprint for a particular object. Every object in the actual world has some colour, shape, and functionality. Similar things can be grouped together in real life depending on certain criteria. A Ford automobile and a Toyota car, for example, are both Cars and can be categorised as such. Thousands of more Cars of the same make and model could exist. Each car was constructed using the same blueprints and so has the same components. Your automobile is an object (instance) of the CAR class, in object-oriented semantics. Because a class is only a template, and objects are concrete instances based on the template, you can make different objects with the same class.

C# Class

A class defines some attributes, fields, events, methods, and so on in object-oriented programming. A class specifies the kind of data that will be stored and the capabilities that their objects will have.

You can construct custom types by grouping variables from other kinds, methods, and events into a class.

Creating a C# Class:

The class keyword is used to create a class in C#.

The class will have some attributes related to the class element.

Example:

C# Class Members:

Class in C# has 2 Fields and 1 Method which are known as the members of the class.

1-Field:

The Class have some variables which have some value in it,

The field is a class-level variable that holds a value. Generally, field members should have a private access modifier and used it with the property.

Example:

In C# to call the field, we have to create an object of the already created class and then use the dot syntax to call them.

2-Methods:

Methods in Class, provide services like calculation or other actions on its members. It is a separate code block, and that contains a series of statements to perform particular operations. 

Syntax:

3-C# Access Modifiers

The declaration of the class, method, properties, fields, and other members are all affected by access modifiers. They specify how accessible the class and its components are. In C#, access modifiers include public, private, protected, and internal. In the keyword section, we’ll discover more about it.

4-Constructor:

Class in C# has special member functions. Constructor is one of them. Constructor is executed when new objects of the class were created. It is used to initialize the objects. It does not have any return type and it has the same name as the class. Constructor Parameters are used to initialize fields in the class. They are also known as a time-saver in C#.

Syntax:
using System;

namespace LineApplication {
  public class Line {
      private double length;   
      
      public Line() {
         Console.WriteLine("Domain is created");
      }
      public void setLength( double len ) {
         length = len;
      }
      public double getLength() {
         return length;
      }

    public  static void Main(string[] args) {
         Line line = new Line();    
         
         line.setLength(8.4);
         Console.WriteLine("Domain Value : {0}", line.getLength());
         
      }
   }
}

5-Destructor:

Destructor is also a special member function. It is executed when an object goes out of scope. It is used to release memory resources before quitting the program. Destructors don’t have any return type and do not take any parameters. It can have the same name as the class.

using System;

namespace LineApplication {
  public class Line {
      private double length; 
      
      public Line() {   // constructor
         Console.WriteLine("Domain is Created");
      }
      ~Line() {   //destructor
         Console.WriteLine("Domain is Deleted");
      }
      public void setLength( double len ) {
         length = len;
      }
      public double getLength() {
         return length;
      }
     public static void Main(string[] args) {
         Line line = new Line();

         // set line length
         line.setLength(8.4);
         Console.WriteLine("Domain Value : {0}", line.getLength());           
      }
   }
}

6-Properties:

Property of any Class in C# associated with some actions like reading and writing. It can contain one or two code blocks known as Accessors. One accessor is called GET and the other one is called the SET.

Syntax:

6- Operators:

Conversions and expression operators are supported by the class. It is a programming element that is used to specify what kind of operation needs to perform on operands or variables. These actions can be +, *, / , – etc.

7- Constants:

In Class, the Constant are the values that are associated with the class. The const keyword is used to declare a constant field, which value can’t be changed.

Syntax: