Notifications
Clear all
Topic starter 01/08/2025 11:41 pm
.NET classes are a powerful way to define blueprints for creating objects in languages like C# and VB.NET—essential for object-oriented programming (OOP).
🏗️ Defining a Class in .NET (C# Example)
public class Dog
{
public string Name { get; set; }
public string Breed { get; set; }
public Dog(string name, string breed)
{
Name = name;
Breed = breed;
}
public void Speak()
{
Console.WriteLine($"{Name} says woof!");
}
}
class
defines the classpublic
makes it accessible from other parts of the program- Properties (
Name
,Breed
) encapsulate data - Methods (
Speak
) define behavior
🐾 Creating an Object
Dog myDog = new Dog("Fido", "Labrador");
myDog.Speak(); // Outputs: Fido says woof!
🔧 Constructors
Used to initialize objects with specific values:
public Dog(string name, string breed)
{
Name = name;
Breed = breed;
}
🧬 Inheritance
Create a class based on another:
public class Puppy : Dog
{
public Puppy(string name, string breed) : base(name, breed) {}
public void Play()
{
Console.WriteLine($"{Name} is playing!");
}
}
💡 Other Key Features
- Access Modifiers:
public
,private
,protected
,internal
control visibility - Static members: belong to the class rather than an instance
- Interfaces: define contracts for what methods a class must implement
- Abstract classes: define partial implementations and must be inherited
- Encapsulation: use properties and access modifiers to manage internal state
.NET makes it super intuitive to build clean, scalable, and maintainable software.