Loading, please wait...

MCQ

Classes and Objects in C# | C# Tutorial

Everything in C# having attributes and properties includes classes and methods. A class is a data type. It stores objects of similar types. It can have fields, methods, constructors, etc. The class creates an object. Each object has its own characteristics. These characteristics are known as properties.

Classes and Objects in C# | C# Tutorial

Everything in C# having attributes and properties includes classes and methods. A class is a data type. It stores objects of similar types. It can have fields, methods, constructors, etc. The class creates an object. Each object has its own characteristics. These characteristics are known as properties.

The objects can be anything like a date, time, etc.

In another word, a class is a unit which stores methods and when we add properties it can be said objects. Simply objects are made from class. For example, if you want to work with student's data in a particular application. The properties of the student would be the roll number and name of the student. The methods would include the entry and modification of student data.

Creating Class

Creating a class requires a class keyword followed by a name for the class.

Example:

class Book 
{

  string color = "red";

}

Generally, the class declaration includes several things such as modifiers, keywords, class identifiers, etc.

The following is a list of different types of data members that can be used in c# classes.

Fields

Variables of the class

Methods

Computations and actions that can be performed by the class

Properties

Actions associated with reading and writing named properties of the class

Events

Notifications that can be generated by the class

Constructors

Actions required to initialize instances of the class or the class itself

Operators

Conversions and expression operators supported by the class

Constants

Constant values associated with the class

Indexers

Actions associated with indexing instances of the class like an array

Finalizers

Actions to perform before instances of the class are permanently discarded

Types

Nested types declared by the class

 

Example:

// declaring public class

public class Show

{

    // field variable

    public int x, y;

      // member function or method

      public void Display()

      {

          Console.WriteLine(“Class & Objects in C#”);

      }

}

In the above code, it is a class that has a declaration and some functions inside it.

Creating Object.

Now that we have a class, we can make an object from it. There can be more than one object for a class. An object consists of:

  • - State
  • - Behaviour
  • - Identity

Here the state means data, behavior means functionality and identity means its uniqueness.

Example:

class Book 

{

  string color = "red";

  static void Main(string[] args)

  {

   Book myObject = new Book();

    Console.WriteLine(myObj.color);

  }

}

In the above code new is used with the class name and saved in a unique named object formed from the class only that is why it is mentioned as

className objectName = new className();

Then we fetched the color property from the Book class and saved it in the object, to check we printed it with Console.WriteLine().

A single constructor is present in every class, we can understand and recognize it by seeing the declaration name, which has the same name as the class. It has no return type.

There are many things we can do with classes and objects. It is like a store that contains all the properties and can be accessed anytime anywhere. Not only that but we can also edit the properties from the classes and insert methods.

Example:

using System;  

   public class Book  

    {  

        public int id;   

        public String name;  

        public void insert(int i, String n)  

        {  

            id = i;  

            name = n;  

        }  

        public void display()  

        {  

            Console.WriteLine(id + " " + name);  

        }  

   }  

   class dataBook{  

       public static void Main(string[] args)  

        {  

            Book b1 = new Book();  

            Book b2 = new Book();  

            b1.insert(101, "Tom and Jerry");  

            b2.insert(102, "Harry Potter");  

            b1.display();  

            b2.display();  

        }  

    }  

In the above code, it is the whole class which is having almost everything, from objects, properties to methods. It can be observed that there is a class named Book in which we are initializing and displaying objects through the method.

MCQ

Related Article