Home  • Programming • C#.NET

Classes in C# Programming

A class defines a type of object.
A class name should be a common noun.
A class is the most powerful data type in C#. Like structures, a class defines the data and behavior of the data type. Programmers can then create objects that are instances of this class. Unlike structures, classes support inheritance, a fundamental part of object-oriented programming.

Declaring Classes

Classes are defined using the class keyword, as shown in the following example:
public class Customer
{
    //Fields, properties, methods and events go here...
}
The class keyword is preceded by the access level. In this case public is used, meaning anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

Creating Objects

Objects can be created using the new keyword followed by the name of the class that the object will be based upon, like this:
Customer object1 = new Customer();
In the example above, object1 is a reference to an object based on Customer. This reference refers to the new object, but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:
Customer object2;
Creating object references like this one that does not refer to an object is not recommended because attempting to access an object through such a reference will fail at run time. However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, like this:
Customer object3 = new Customer();
Customer object4 = object3;
This code creates two object references that both refer to the same object. Therefore, any changes to the object made through object3 will be reflected in subsequent uses of object4. It is because objects based on classes are referred to by reference that classes are known as reference types.

Class Inheritance

Inheritance is accomplished through the use of a derivation, which means a class is declared using a base class from which it inherits data and behavior. A base class is specified by appending a colon and the name of the base class following the derived class name, like this:
public class Manager : Employee
{
    // Employee fields, properties, methods and events are inherited
    // New Manager fields, properties, methods and events go here...
}
When a class declares a base class, all of the class members defined for the base class become part of the new class as well. Because a base class may itself inherit from another class, which inherited from another class, and so on, a class may end up with many base classes.

Example

In the following example, a public class is defined, containing a single field, a method and a special method called a constructor. For more information, see Constructors. The class is then instantiated with the new keyword.
public class Person
{
    // Field
    public string name;

    // Constructor
    public Person()
    {
        name = "unknown";
    }

    // Method
    public void SetName(string newName)
    {
        name = newName;
    }
}
class TestPerson
{
    static void Main()
    {
        Person person1 = new Person();
        System.Console.WriteLine(person1.name);

        person1.SetName("Jahidul Islam");
        System.Console.WriteLine(person1.name);
    }
}

Output

unknown Jahidul Islam

Comments 2


The best description of OOP
very help

Share

Copyright © 2024. Powered by Intellect Software Ltd