Easy csharp

OOP — Constructor initialization #2

Problem

Use constructor to set bank account balance.

Hints
  • public ClassName(params) { ... }

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program
{
    static void Main()
    {
        var acc = new BankAccount(1000);
        Console.WriteLine(acc.Balance);

class BankAccount {
    public decimal Balance { get; }
    public BankAccount(decimal initial) => Balance = initial;
}
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Constructor initializes object state—core OOP concept.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details