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 42 of 55
←PreviousPrevNextNext→

C# Class Members - Complete Guide

Introduction to Class Members

Class members in C# define the structure, data, and behavior of a class. They include fields, properties, methods, events, constructors, and nested types. Each member can have an access modifier that controls how it can be accessed from other parts of the code.

Understanding class members is essential in object-oriented programming because they allow you to encapsulate state and functionality inside objects, making programs more modular and maintainable.

Types of Class Members

C# provides multiple kinds of class members, each serving a different purpose. The example below shows common types of members in a single class:

Example
using System;

namespace ClassMembersExample
{
    public class Person
    {
        // Field (private backing data)
        private string name;
        
        // Property (public access to data)
        public int Age { get; set; }
        
        // Method (behavior)
        public void Greet()
        {
            Console.WriteLine($"Hello, my name is {name} and I am {Age} years old.");
        }
        
        // Constructor (initialization)
        public Person(string name, int age)
        {
            this.name = name;
            Age = age;
        }
        
        // Event (notification mechanism)
        public event EventHandler AgeChanged;
        
        // Nested class (type inside another class)
        public class Address
        {
            public string Street { get; set; }
            public string City { get; set; }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person("Alice", 30);
            person.Greet();
            
            person.Age = 31;
            person.Greet();
            
            // Using nested class
            Person.Address address = new Person.Address
            {
                Street = "123 Main St",
                City = "Springfield"
            };
            
            Console.WriteLine($"Address: {address.Street}, {address.City}");
        }
    }
}
Output
Hello, my name is Alice and I am 30 years old.
Hello, my name is Alice and I am 31 years old.
Address: 123 Main St, Springfield
Test your knowledge: C# Class Members - Complete Guide
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# OOPTopic 42 of 55
←PreviousPrevNextNext→