Monday, November 1, 2021

C# Class As a Type

Main points about class

  • Class is a template to create objects.
  • Class is user defined data type.
  • Class represents a business entity.
  • Class is composed of fields, properties and methods.
  • Fields of a class represent data of its objects.
  • Methods of a class represent behavior of its objects. 
  • Properties represent cross over between data and behavior of objects of a class.
  • Every class must have constructor(s) to create objects.

Introduction

Understanding Classes and Objects in C#

C# is an object-oriented programming (OOP) language where objects are the centerpiece of programming. In OOP, programs are written around objects. The objects collectively determine the flow and outcome of the program.

In OOP, an object is the fundamental unit of a program. Just as a building is made up of bricks, in object-oriented programming, objects are the foundational building blocks. Similarly, just as a template or mold is used to create bricks, object-oriented programming uses templates to create different types of objects. These templates are called classes in OOP languages.

We are familiar with built-in data types like int, string, and bool. These data types define what kind of data a variable can hold and what operations can be performed on that data. While built-in data types are predefined, a class is a user-defined type created by programmers to design objects based on specific requirements. C# allows programmers to define classes to create objects from them.

A class is essentially a template that contains fields (to store data) and methods (to perform operations on that data).

Key Features of a Class

In other words, a class is a collection of fields, properties, and methods:

  • Fields: Store the data of the objects created from the class.
  • Methods: Perform operations on the data stored in the fields.
  • Properties: Provide controlled access to the data.

Once a class is defined, multiple objects can be created from it. Constructors are used within a class to initialize the fields of its objects. We will study constructors in detail in a separate post.

Using Classes for Problem Solving

The purpose of programming is often to solve business problems. To achieve this, programmers identify the business entities (or objects) relevant to the problem, outline their attributes, and model them using classes.

For example, in a school management system, entities like teachers, students, and staff represent different business objects. A Teacher class could have fields like Name, Age, Qualification, and JoiningDate.

Here is a simplified representation of a Teacher class:

public class Teacher

{

    int Id;

    string Name;

    int Age;

    string Qualification;

    string JoiningDate;

}

Notes on Class Syntax:

  • The class name starts with an uppercase letter and follows the class keyword.
  • Classes can have access specifiers like public or internal.
    • A public class is accessible across all projects in an application.
    • An internal class is accessible only within the same project.
  • Members (fields, methods, etc.) are defined inside curly braces {}.

Adding Methods to a Class

Classes typically include methods that define the behavior of the objects. For example, if a teacher updates their qualification, the Qualification field of the Teacher object must change.

Below is an enhanced version of the Teacher class that includes methods to update qualifications and age:

public class Teacher

{

    int Id;

    string Name;

    int Age;

    string Qualification;

    string JoiningDate;

 

    public void UpdateQualification(string newQualification)

    {

        Qualification = newQualification;

    }

 

    public void UpdateAge(int newAge)

    {

        Age = newAge;

    }

}

Creating Objects from a Class

Objects of a class are created using the new keyword, which invokes the class constructor. Here is an example:

public class Teacher

{

    string Name;

 

    public void SetName(string name)

    {

        this.Name = name;

    }

 

    public void GetName()

    {

        Console.WriteLine("Teacher is " + this.Name);

    }

}

 

class Program

{

    static void Main(string[] args)

    {

        Teacher teacher1 = new Teacher();

        teacher1.SetName("Ajeet");

        teacher1.GetName();

 

        Teacher teacher2 = new Teacher();

        teacher2.SetName("Rakesh");

        teacher2.GetName();

    }

}

Reference Variables and Objects

The new operator creates an object in the heap, while the reference variable (e.g., teacher1) is created on the stack. The reference variable points to the object in memory.

For instance, the statement:

Teacher teacher1 = new Teacher();

has two components:

  • The RHS (new Teacher()) creates the object in the heap.
  • The LHS (teacher1) is the reference variable pointing to the object.

Types of Classes in C#

Classes in C# can be classified based on their access modifiers or their nature (static or non-static).

Based on Access Modifiers:

  1. Public Class: Accessible from any project within the application.
  2. Internal Class: Accessible only within the project where it is defined.

Based on Nature:

  1. Static Class:
    • Contains only static members (fields, properties, and methods).
    • Cannot be instantiated.
    • Has a static constructor, which is parameterless and initializes static members.
  2. Non-Static Class:
    • Can have both static and non-static members.
    • Can be instantiated to create multiple distinct objects.
    • Can have multiple constructors, including parameterized constructors.

Example of a Static Class:

public static class MathHelper

{

    public static int Add(int a, int b)

    {

        return a + b;

    }

}

Example of a Non-Static Class:

public class Calculator

{

    public int Add(int a, int b)

    {

        return a + b;

    }

}

Conclusion

C#'s object-oriented features, such as classes and objects, provide a powerful way to model and solve real-world problems. By defining classes with fields, properties, and methods, programmers can encapsulate the data and behavior of business entities, making their programs modular and easy to maintain.

No comments:

Post a Comment

Hot Topics