DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# OOPTopic 40 of 55
←PreviousPrevNextNext→

C# Classes and Objects - Complete Guide

Introduction to Classes and Objects

In C#, a class is a template that defines the structure and behavior of objects. It can contain fields, properties, methods, and constructors. An object is a runtime instance of a class, created using the 'new' keyword.

Classes are a cornerstone of object-oriented programming (OOP) in C#. They support encapsulation, inheritance, and polymorphism, helping organize code into reusable and maintainable units.

Class Declaration and Object Creation

You declare a class with the 'class' keyword and create objects from it using 'new'. Each object has its own copy of the class's fields and properties, but methods are shared definitions.

Example
using System;

// Class declaration
public class Car
{
    // Fields
    public string Make;
    public string Model;
    public int Year;
    
    // Method
    public void DisplayInfo()
    {
        Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Car car1 = new Car();
        car1.Make = "Toyota";
        car1.Model = "Camry";
        car1.Year = 2022;
        
        Car car2 = new Car();
        car2.Make = "Honda";
        car2.Model = "Civic";
        car2.Year = 2021;
        
        Console.WriteLine("Car 1 Information:");
        car1.DisplayInfo();
        
        Console.WriteLine("\nCar 2 Information:");
        car2.DisplayInfo();
        
        // Objects are reference types
        Car car3 = car1;
        car3.Year = 2023;
        
        Console.WriteLine("\nAfter modifying car3:");
        Console.WriteLine("Car 1 year: " + car1.Year);
        Console.WriteLine("Car 3 year: " + car3.Year);
    }
}
Output
Car 1 Information:
Make: Toyota, Model: Camry, Year: 2022

Car 2 Information:
Make: Honda, Model: Civic, Year: 2021

After modifying car3:
Car 1 year: 2023
Car 3 year: 2023

Class Members and Access Modifiers

Classes can define members such as fields, properties, methods, and constructors. Access modifiers like public and private control visibility.

Public members are accessible from outside the class, while private members are hidden and only accessible within the class itself.

Example
using System;

public class Person
{
    private string name;
    private int age;
    
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    
    public int Age
    {
        get { return age; }
        set { age = value; }
    }
    
    public Person(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
    
    public void Introduce()
    {
        Console.WriteLine($"Hello, my name is {name} and I'm {age} years old.");
    }
    
    private bool IsAdult()
    {
        return age >= 18;
    }
    
    public void CheckAdulthood()
    {
        if (IsAdult())
            Console.WriteLine("I am an adult.");
        else
            Console.WriteLine("I am not an adult yet.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person person = new Person("Alice", 25);
        person.Introduce();
        person.CheckAdulthood();

        person.Name = "Bob";
        person.Age = 16;

        Console.WriteLine("\nAfter changes:");
        person.Introduce();
        person.CheckAdulthood();

        // person.IsAdult(); // Error: inaccessible due to private modifier
    }
}
Output
Hello, my name is Alice and I'm 25 years old.
I am an adult.

After changes:
Hello, my name is Bob and I'm 16 years old.
I am not an adult yet.
Test your knowledge: C# Classes and Objects - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# OOPTopic 40 of 55
←PreviousPrevNextNext→