In C#, classes and objects are used to define and create custom data types that encapsulate data and behavior into a single unit. Here are some of the key concepts related to classes and objects in C#:
1. Classes: A class is a blueprint for creating objects that share the same properties and behaviors. A class can contain fields, properties, methods, constructors, and other constructs.
Here is an example of a simple class that represents a person:
public class Person { public string Name; public int Age; public void SayHello() { Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old."); } }
This class has two fields, “Name” and “Age”, and a method named “SayHello” that prints a greeting to the console.
2. Objects: An object is an instance of a class. When you create an object, you are creating a specific instance of the class that has its own values for the fields and properties of the class.
Here is an example of how to create an object of the “Person” class:
Person person1 = new Person(); person1.Name = "Alice"; person1.Age = 25; person1.SayHello();
This code creates a new instance of the “Person” class named “person1”, sets the values of its fields “Name” and “Age”, and calls the “SayHello” method to print a greeting to the console.
3. Constructors: A constructor is a special method that is used to create and initialize objects of a class. Constructors have the same name as the class and can have parameters to set the initial values of the fields and properties of the object.
Here is an example of a class with a constructor:
public class Person { public string Name; public int Age; public Person(string name, int age) { Name = name; Age = age; } public void SayHello() { Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old."); } }
This class has a constructor that takes two parameters, “name” and “age”, and sets the values of the fields “Name” and “Age” of the object.
4. Access modifiers: C# defines several access modifiers that control the accessibility of fields, properties, and methods of a class. The available access modifiers are public, private, protected, internal, and protected internal.
Here is an example of a class with different access modifiers:
public class Person { public string Name; // public field private int Age; // private field public Person(string name, int age) { Name = name; Age = age; } public void SayHello() // public method { Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old."); } private void DisplayAge() // private method { Console.WriteLine("My age is " + Age); } }
In this example, the “Name” field and the “SayHello” method are public, which means they can be accessed from outside the class. The “Age” field and the “DisplayAge” method are private, which means they can only be accessed from within the class.
Classes and objects are fundamental to writing object-oriented C# code. By defining classes and creating objects, you can encapsulate data and behavior into a single unit and build complex systems that are easy to maintain and extend.