Understanding C# Classes and Objects
In C#, classes and objects are fundamental concepts of object-oriented programming (OOP). A class is a blueprint or template for creating objects, providing initial values for state (fields) and implementations of behavior (methods). An object is an instance of a class.
Key Topics
Classes and Objects Overview
A class in C# is a user-defined type that serves as a blueprint for objects. It encapsulates data for the object and methods to manipulate that data. An object is an instance of a class that contains actual values.
Defining a Class
You can define a class using the class
keyword followed by the class name. The class body contains fields, methods, and other members.
Example: Creating a Simple Class
public class Person
{
// Fields
public string Name;
public int Age;
// Method
public void Introduce()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
Explanation: The Person
class has two fields, Name
and Age
, and a method Introduce()
that outputs a greeting message.
Creating Objects
Objects are created using the new
keyword followed by the class name and parentheses. You can then access the object's fields and methods using the dot notation.
Example: Instantiating an Object
public class Person
{
// Fields
public string Name;
public int Age;
// Method
public void Introduce()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
class Program
{
static void Main(string[] args)
{
// Create a new Person object
Person person1 = new Person();
person1.Name = "Alice";
person1.Age = 30;
person1.Introduce();
}
}
Output:
Hello, my name is Alice and I am 30 years old.
Explanation: The class Person
is defined, and then an instance person1
is created. We assign values to its fields and call the Introduce()
method.
Working with Multiple Objects
You can create multiple objects from the same class, each with its own set of data.
Example: Creating Multiple Instances
public class Person
{
// Fields
public string Name;
public int Age;
// Method
public void Introduce()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
class Program
{
static void Main(string[] args)
{
// First Person object
Person person1 = new Person();
person1.Name = "Alice";
person1.Age = 30;
person1.Introduce();
// Second Person object
Person person2 = new Person();
person2.Name = "Bob";
person2.Age = 25;
person2.Introduce();
}
}
Output:
Hello, my name is Alice and I am 30 years old. Hello, my name is Bob and I am 25 years old.
Explanation: The Person
class is defined, and two instances, person1
and person2
, are created. Each object has its own values for Name
and Age
, and they both use the Introduce()
method.
Key Takeaways
- A class is a blueprint for creating objects.
- An object is an instance of a class containing actual values.
- Use the
new
keyword to create an object. - Access object members using the dot notation.
- You can create multiple objects from the same class.