Understanding Indexer

 Indexer in c# is same as Properties. They contain both get and set methods just like Properties. Question arises if they are similar as Properties then why do we indexer?well, we all know that by default class variables are private and cannot be accessed outside of its class. if you want to maintain private-ness of the variables inside the class and want to access those variables outside of class then we have only two options

Declare it as public - this is not safe

Declare it as properties - this is safe choice

Now if  you don’t want to declare the above two points but still want to access class variables then the other is declaring indexer inside the class. So if you declare indexer inside any class then that class will become or act like VIRTUAL ARRAY.





Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code