pcbway





introduction to C# classes, intro to C# classes, basics of C# classesHi Everyone! Hope you all are doing great. We always love when you keep coming back for what we have to offer. Today, I am going to give you a Introduction to C# Classes. We have already seen Introduction to Data Types in C# which includes boolean, integer, char and double. However, if you want to make complex custom type, you can make use of classes. I’ll give you a brief introduction to classes so you don’t have to go anywhere to find information regarding classes in C#. Let’s hop on the board and dive in the details of classes.

Introduction to C# Classes

  • Class in C# is referred as a blueprint of complex data type that describes the behavior and data of type.
  • If you want to create complex custom type, we use class. What do I mean by complex custom type?
  • Suppose you want to store a number 123, you can store it easily in an integer variable.
  • However, if you want to store the customer’s information, you can use classes like what kind of information customer has, what is his first and last name, his email ID, phone number and age etc.
  • By using a built-in fields we can create a class.
  • Fields of class define the class data and methods define the behavior of the class.
  • When we create customer class, it not only contains the data, it can also do certain things like save the customer to the data base, print the customer full name etc.
  • So, class has state and behavior, state is nothing but data and behavior is nothing but what the class is capable of doing.
How to Create a Class

In order to create a class we use a class keyword followed by the name of the class and members of class are enclosed by curly brackets. Let’s look at an example which will make things clear.

Example 1

Following figure shows the complete explanation of classes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Customer
{
    string _firstName;
    string _lastName;

    public Customer (string FirstName, string LastName)
{
        this._firstName = FirstName;
      this._lastName = LastName;
}
    public void PrintFullName()
    {
        Console.WriteLine("Full Name = {0}", this._firstName +" "+ this._lastName);
    }
    ~Customer()
    {
        //clean up code
    }
    
}
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer C1 = new Customer("Adnan", "Network");
            C1.PrintFullName();
        }
    }
}
  • In the above example first name and last name of the customer is stored in a string which represents the state of the class.
  • To initialize these fields a class can also have an constructor.
  • A constructor will have a same name as that of your class.
  • We make constructor public which is an access modifier.
  • Constructors are basically used to initialize the class fields.
  • Then we pass two parameters into this constructor which are string FirstName and string LastName.
  • Constructor doesn’t return a value but it can take parameters.
  • Then we use constructor to initialize these class fields i.e _firstname and _lastname.
  • This field _firstname is equal to the parameter FirstName and the field _lastname is equal to the parameter LastName.
  • Then we put “this” keyword before firstName and lastName which is used to define the instance of classes also referred as objects.
  • So, basically we are using constructor to initialize the class fields with parameters that are passing into the constructor.
  • In the next step we add a behavior which allows the class to do something.
  • We want to print the full name of the customer. This is called the method of the class which tells the behavior of the class.
  • So far, we have created constructor, two fields and a methods for the class.
  • In the next step we call the destructor of the class.
  • Destructor will have the same name as that of the class but it doesn’t take parameters and doesn’t have a return type.
  • Usually in C# we don’t require destructors.
  • We use destructors to clean up the resources your class was holding on to during its life time.




  • We don’t need to call the destructors, they are automatically called by the garbage collector.
  • In the next step we make use of class in our main method.
  • In this step we create instance C1 of class Customer with using the keyword “new”.
  • This instance is also called the object of the class. Instances and Objects are used interchangeably.
  • Then we call FirstName and LastName of the customer. When we put FirstName as Adnan, it will pass through the parameter (string FirstName) of the constructor. And put second name as Network that will pass through the parameter(string LastName) of the constructor.
  • Constructors are called automatically when you create instance of class.
  • Also, constructors are not mandatory to use, if you don’t provide a constructor, .NET framework will automatically provide a default parameter less constructor.
  • In the next step we print the full name of the customer.

Output

Output of the above program will be like below.

introduction to C# classes, intro to C# classes, basics of C# classes

That’s all for today. I hope you have got a clear idea about class, constructor and destructor used in C#. However, if still you feel any doubt or have any question you can ask me in the question below. I’ll be happy to help you in this regard according to best of my expertise. Stay tuned!

Leave a Reply