Understanding C# Classes and Objects: A Comprehensive Guide
Written on
Chapter 1: Introduction to Classes in C#
In C#, a class serves as a fundamental component of Object-Oriented Programming (OOP), acting as a blueprint for creating instances. Each class can encompass various elements such as fields, properties, methods, constructors, and destructors.
When you instantiate a class, you're creating an object that allows you to access its fields, methods, properties, and constructors (which can accept either no arguments or multiple arguments). Notably, classes are reference types and are stored in the heap memory.
Here’s a brief illustration of declaring and initializing data members in a class:
// Fields (attributes or variables)
private int FruitPrice;
private string FruitName;
private bool FruitIsInMarket;
You can initialize these data members as follows:
// Initializing data members of the class
FruitPrice = 100;
FruitName = "Mango";
FruitIsInMarket = true;
Section 1.1: Constructors and Destructors
A constructor is a special method invoked when an object of the class is created:
// Constructor
public Fruit(int price, string name, bool isAvailable)
{
FruitPrice = price;
FruitName = name;
FruitIsInMarket = isAvailable;
}
Conversely, a destructor is called when an object is destroyed:
// Destructor
~Fruit()
{
Console.WriteLine("Fruit Instance is Destroyed");
}
Section 1.2: Methods in Classes
Methods define the behaviors of a class. Here’s how to declare a method:
// Methods (functions or behaviors)
public void GetFruit()
{
Console.WriteLine("This is a collection of fruits");
}
Creating an Instance of a Class
To create an instance of a class in C#, you can do so like this:
Fruit objFruit = new Fruit(20, "Mango", true); // Creating an instance of the Fruit class
objFruit.GetFruit(); // Calling a method
Chapter 2: Practical Examples
The video titled "C Programming Tutorial for Beginners" provides an insightful introduction to the basics of programming, which can be essential for understanding concepts like classes in C#.
Here's a practical example demonstrating the use of classes and objects:
using System;
public class Program
{
public static void Main(string[] args)
{
Fruit objFruit = new Fruit(20, "Mango", true);
objFruit.GetFruit(); // Calling a method
int fieldPrice = objFruit.GetFruitPrice(); // Accessing a field fruit price
string fieldName = objFruit.GetFruitName(); // Accessing a field fruit name
bool fieldIsInMarket = objFruit.GetFruitIsInMarket(); // Accessing a field fruit is in market
Console.WriteLine("Fruit name: " + fieldName + " Fruit price: " + fieldPrice + " Is available in market: " + fieldIsInMarket);
}
}
public class Fruit
{
// Fields (attributes or variables)
private int FruitPrice;
private string FruitName;
private bool FruitIsInMarket;
// Constructor
public Fruit(int price, string name, bool isAvailable)
{
FruitPrice = price;
FruitName = name;
FruitIsInMarket = isAvailable;
}
// Methods (functions or behaviors)
public void GetFruit()
{
Console.WriteLine("This is a collection of fruits");}
public int GetFruitPrice()
{
return FruitPrice;}
public string GetFruitName()
{
return FruitName;}
public bool GetFruitIsInMarket()
{
return FruitIsInMarket;}
}
Output
When the code runs, it will display:
This is a collection of fruits
Fruit name: Mango Fruit price: 20 Is available in market: True
Summary
In conclusion, classes are the cornerstone of C# and Object-Oriented Programming. They help maintain organized and reusable code, making it easier to manage real-world entities within your applications through their fields, methods, constructors, and destructors.
For additional valuable tutorials, please visit C-Sharp Tutorial. If you found this article helpful, kindly show your support by clapping and following the author.