Ranganatha
Ranganatha

Reputation: 1176

Why delegates defined in the class are not accessed by class object

I have got some BankAccount like below.

My question is using instance of BankAccount class, I can access the ActionDelegate delegate declared in the class but the not BalanceChangedEventHandler delegate defined in the class?

public class BankAccount
{
    public Action<int> ActionDelegate;

    public delegate void BalanceChangedEventHandler(object sender, BalanceChangedEventArgs args);

    public event BalanceChangedEventHandler BalanceChanged;
}

Upvotes: 3

Views: 81

Answers (2)

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

You can access the delegate via the class name not an instance of this class

so you have to write

BankAccount.BalanceChangedEventHandler

Upvotes: 0

Heinzi
Heinzi

Reputation: 172280

(emphasis mine)

... using instance of BankAccount class, I can access [...] but the not BalanceChangedEventHandler delegate defined in the class?

Delegate declarations don't have state. Thus, you don't access them through the instance, you access them like a nested class declaration:

 myBankAccount.BalanceChangedEventHandler // won't work
 BankAccount.BalanceChangedEventHandler   // works

Upvotes: 2

Related Questions