C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Class

In this tutorial, we'll cover the basics of classes in C#. A class is a blueprint for creating objects, and it defines the data and behavior of an object. Classes can contain fields, properties, methods, constructors, and more.

  • Set up the environment

Create a new C# Console Application project in Visual Studio.

  • Create a sample class

Create a sample class called Person with the following members: a field, a property, a method, and a constructor:

public class Person
{
    // Field
    public string Name;

    // Property
    public int Age { get; set; }

    // Constructor
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    // Method
    public void Greet()
    {
        Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
    }
}

In this example, the Person class has a field Name, a property Age, a constructor that takes name and age as parameters, and a method Greet that displays a greeting message.

  • Instantiate the class

In the Main method, create an instance of the Person class:

static void Main(string[] args)
{
    // Instantiate the Person class
    Person person = new Person("John Doe", 30);

    // Call the method
    person.Greet();

    Console.ReadLine();
}

When you run the Console Application, you should see the following output:

Hello, my name is John Doe and I am 30 years old.
  • Class members explained

Let's briefly explain the different types of class members used in this example:

  • Field: A field is a variable that is declared directly in a class. Fields are used to store data for an object. In our example, Name is a field.

  • Property: A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. In our example, Age is a property.

  • Constructor: A constructor is a special method that is called when an object is created. It is used to initialize the object's fields and properties with default or provided values. In our example, Person(string name, int age) is a constructor.

  • Method: A method is a block of code that performs a specific action or calculation. Methods can be called on objects to execute their functionality. In our example, Greet() is a method.

In this tutorial, we've covered the basics of classes in C#, including fields, properties, constructors, and methods. Classes are fundamental building blocks in object-oriented programming and are essential for creating objects and organizing your code in C#.

  1. C# class definition example:

    • Description: This example introduces the basic structure of a class in C#.
    • Code:
      public class MyClass
      {
          // Class members go here
      }
      
  2. Creating and using classes in C#:

    • Description: Demonstrating how to create an instance of a class and use its members in C#.
    • Code:
      public class MyClass
      {
          public void DisplayMessage()
          {
              Console.WriteLine("Hello from MyClass!");
          }
      }
      
      // Usage
      MyClass myObject = new MyClass();
      myObject.DisplayMessage();
      
  3. Class constructor in C#:

    • Description: Illustrating how to define a constructor within a class in C#.
    • Code:
      public class MyClass
      {
          // Constructor
          public MyClass()
          {
              Console.WriteLine("Constructor called!");
          }
      }
      
      // Usage
      MyClass myObject = new MyClass(); // Constructor called!
      
  4. C# class properties and methods:

    • Description: Introducing both properties and methods within a class in C#.
    • Code:
      public class MyClass
      {
          // Property
          public string MyProperty { get; set; }
      
          // Method
          public void DisplayProperty()
          {
              Console.WriteLine($"Property value: {MyProperty}");
          }
      }
      
      // Usage
      MyClass myObject = new MyClass();
      myObject.MyProperty = "Hello, World!";
      myObject.DisplayProperty(); // Output: Property value: Hello, World!
      
  5. Encapsulation in C# classes:

    • Description: Discussing the concept of encapsulation and how to use access modifiers to control the visibility of class members.
    • Code:
      public class MyClass
      {
          // Private field
          private string myField;
      
          // Public property with encapsulation
          public string MyProperty
          {
              get { return myField; }
              set { myField = value; }
          }
      }
      
  6. Inheritance in C# classes:

    • Description: Demonstrating how to create a derived class that inherits from a base class in C#.
    • Code:
      public class MyBaseClass
      {
          public void BaseMethod()
          {
              Console.WriteLine("Base method called!");
          }
      }
      
      public class MyDerivedClass : MyBaseClass
      {
          // Additional members in the derived class
      }
      
      // Usage
      MyDerivedClass derivedObject = new MyDerivedClass();
      derivedObject.BaseMethod(); // Output: Base method called!
      
  7. Abstract classes in C#:

    • Description: Introducing abstract classes and methods, which provide a blueprint for derived classes to implement.
    • Code:
      public abstract class MyAbstractClass
      {
          // Abstract method without implementation
          public abstract void AbstractMethod();
      }
      
      public class MyDerivedClass : MyAbstractClass
      {
          // Implementing the abstract method
          public override void AbstractMethod()
          {
              Console.WriteLine("Abstract method implemented in the derived class.");
          }
      }
      
      // Usage
      MyAbstractClass abstractObject = new MyDerivedClass();
      abstractObject.AbstractMethod(); // Output: Abstract method implemented in the derived class.
      
  8. Static classes in C#:

    • Description: Introducing static classes, which cannot be instantiated and can contain only static members.
    • Code:
      public static class MyStaticClass
      {
          public static void StaticMethod()
          {
              Console.WriteLine("Static method called!");
          }
      }
      
      // Usage
      MyStaticClass.StaticMethod(); // Output: Static method called!