A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 7.1: Classes and objects

Creating classes and objects is a fundamental concept in C# programming. In this tutorial, I'll guide you through the basics of defining classes, creating objects, and working with them in C#.

1. Introduction to Classes and Objects:

  • Class: A class is a blueprint or a template for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of the class will have.
  • Object: An object is an instance of a class. It is a concrete realization of the class, with specific values assigned to its properties.

2. Creating a Class:

  • To create a class, use the class keyword, followed by the class name. Here's a simple example:

    using System;

    class Car
    {
        // Properties
        public string Model;
        public int Year;

        // Method
        public void Start()
        {
            Console.WriteLine("Engine started!");
        }
    }

3. Creating Objects:

  • Once you have a class, you can create objects (instances) of that class. Use the new keyword followed by the class name and parentheses.

    class Program
    {
        static void Main()
        {
            // Creating objects
            Car myCar = new Car();
            Car yourCar = new Car();

            // Assigning values to object properties
            myCar.Model = "Toyota";
            myCar.Year = 2020;

            yourCar.Model = "Honda";
            yourCar.Year = 2022;

            // Calling methods
            myCar.Start();
            yourCar.Start();
        }
    }

4. Access Modifiers:

  • Use access modifiers (public, private, protected, etc.) to control the visibility of class members. In the example above, properties and methods are public, meaning they can be accessed from outside the class.

5. Constructor:

  • A constructor is a special method that is called when an object is created. It's used to initialize object properties. If you don't define a constructor, C# provides a default one.

    class Car
    {
        // Properties
        public string Model;
        public int Year;

        // Constructor
        public Car(string model, int year)
        {
            Model = model;
            Year = year;
        }

        // Method
        public void Start()
        {
            Console.WriteLine("Engine started!");
        }
    }

6. Encapsulation:

  • Encapsulation is the bundling of data (properties) and methods that operate on the data into a single unit (class). It helps in data hiding and abstraction.

7. Inheritance:

  • Inheritance allows a class (subclass/derived class) to inherit the properties and methods of another class (base class).

8. Polymorphism:

  • Polymorphism allows objects of different classes to be treated as objects of a common base class. It includes method overloading and method overriding.

Conclusion:

This tutorial covers the basics of classes and objects in C#. Understanding these concepts is crucial for building scalable and maintainable software. As you progress in your C# journey, you'll encounter more advanced topics related to object-oriented programming.