🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Part 6: Object-Oriented Programming (OOP) in C#

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Object-Oriented Programming (OOP) is a paradigm that organises code around objects and classes, making it easier to develop modular and reusable applications. C# is designed to fully leverage this paradigm. In this guide, we will explore the basics of OOP, constructors, and properties in C#.






1. Basic Concepts of OOP






1.1 Classes and Objects



A class is a blueprint that defines the properties and behaviours of an object. Objects are instances of classes.






Example of a Class and Object






CODE
public class Conferences
{
public string Name;
public int PeopleNumber;

public void Welcome()
{
Console.WriteLine($"Hello, this is {Name} and we have {PeopleNumber} people.");
}
}

// Creating an object
Conferences conferences = new Conferences();
conferences.Name = "Codú";
conferences.PeopleNumber = 30;
person.Welcome();






In this example, Conferences is the class, and conferences is the object created from that class.









1.2 Encapsulation



Encapsulation protects a class's data by restricting direct access to its properties and exposing controlled methods to modify or access them. This is achieved using access modifiers like private and public.






Example of Encapsulation






CODE
public class BankAccount
{
private decimal balance;

public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
Console.WriteLine($"Deposit successful. Current balance: {balance}");
}
else
{
Console.WriteLine("The amount must be greater than zero.");
}
}

public void DisplayBalance()
{
Console.WriteLine($"Current balance: {balance}");
}
}

// Creating an object and using methods
BankAccount account = new BankAccount();
account.Deposit(2000);
account.DisplayBalance();






In this example, the balance is protected and can only be modified through the Deposit method.






2. Constructors and Properties






2.1 Constructors



A constructor is a special method that is automatically called when an object is created. It is used to initialise properties.






Example of a Constructor






CODE
public class Car
{
public string Brand;
public string Model;

// Constructor
public Car(string brand, string model)
{
Brand = brand;
Model = model;
}

public void DisplayInfo()
{
Console.WriteLine($"Car: {Brand} {Model}");
}
}

// Creating an object using the constructor
Car myCar = new Car("Audi", "a7");
myCar.DisplayInfo();









2.2 Properties



Properties provide a way to safely access and modify private fields. They are defined using get and set.






Example of Properties






CODE
public class Product
{
private decimal price;

public decimal Price
{
get { return price; }
set
{
if (value >= 0)
{
price = value;
}
else
{
Console.WriteLine("Price cannot be negative.");
}
}
}
}

// Creating an object and using properties
Product product = new Product();
product.Price = 150;
Console.WriteLine($"Product price: {product.Price}");









3. Practical Example: Creating Classes and Objects



Let’s create a Rectangle class that calculates the area and perimeter of a rectangle.




CODE
public class Rectangle
{
public double Length { get; set; }
public double Width { get; set; }

public Rectangle(double length, double width)
{
Length = length;
Width = width;
}

public double CalculateArea()
{
return Length * Width;
}

public double CalculatePerimeter()
{
return 2 * (Length + Width);
}
}

// Creating an object and using its methods
Rectangle rect = new Rectangle(5.0, 3.5);
Console.WriteLine($"Area: {rect.CalculateArea()}");
Console.WriteLine($"Perimeter: {rect.CalculatePerimeter()}");









4. Advancing in OOP: Inheritance and Polymorphism






4.1 Inheritance



Inheritance allows a class (subclass) to inherit properties and methods from another class (superclass).






Example of Inheritance






CODE
public class Animal
{
public string Name { get; set; }

public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}

public class Dog : Animal
{
public void Bark()
{
Console.WriteLine($"{Name} is barking.");
}
}

// Using inheritance
Dog myDog = new Dog();
myDog.Name = "Toby";
myDog.Eat();
myDog.Bark();









4.2 Polymorphism



Polymorphism allows methods to have different behaviours depending on the context in which they are used.






Example of Polymorphism






CODE
public class Bird
{
public virtual void Fly()
{
Console.WriteLine("The bird is flying.");
}
}

public class Eagle : Bird
{
public override void Fly()
{
Console.WriteLine("The eagle flies at great heights.");
}
}

// Using polymorphism
Bird myBird = new Eagle();
myBird.Fly();






Object-Oriented Programming in C# enables you to write organised, reusable, and maintainable code. Mastering concepts such as classes, objects, encapsulation, constructors, properties, inheritance, and polymorphism is essential for developing robust and scalable applications.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Part 6: Object-Oriented Programming (OOP) in C#

Thematisch verwandte Begriffe: Part, ObjectOriented, Programming · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...