Menu

Class, object, fields, properties and methods — OOP Concepts in C#
📰

Class, object, fields, properties and methods — OOP Concepts in C#

DEV Community: dotnet·DIVAKAR UPADHYAY·4 days ago
#i8fRRhyY
#devtodotnet#devto#dev#class#object
Reading 0:00
15s threshold

Lesson 1 of 15 in the free OOP Concepts in C# course on DevLadder.

A class is a blueprint; an object is an instance created from it at runtime.

public class BankAccount { private decimal _balance ; // field (state) — private public string Owner { get ; } // read-only property public decimal Balance => _balance ; // computed property

public BankAccount ( string owner , decimal opening = 0 ) // constructor { Owner = owner ; _balance = opening ; }

public void Deposit ( decimal amount ) // method (behaviour) { if ( amount <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( amount )); _balance += amount ; } }

var acc = new BankAccount ( "Divakar" , 500 ); // object acc . Deposit ( 250 ); Console . WriteLine ( acc . Balance ); // 750

Enter fullscreen mode

Exit fullscreen mode

Interview questions on OOP Concepts in C

What are the four pillars of OOP? Explain each with an example.

Encapsulation — private state with a controlled public API.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.