The generous C#

I found out these amazing facts about C# in the recent few days.

1. new has three different meanings
new operator
new modifier
new constraint

2. readonly keyword is used for variables which are to be initialized only once in the lifetime of the variable. readonly variables can be only initialized when they are declared OR in the constructor.

I was curious about the new modifier applied to members/methods in the derived class which hides members/methods of the base class. As I delved deeper, I realized that the base class implementer has so much control over how its derived classes will behave. More specifically, a base class can enforce these restrictions on its derived classes:

1. Do not allow inherited classes (sealed)
2. Implement a method (abstract)
3. Do not override a method (sealed)

The features that C# provides is much more than one can use. The compiler volunteers to perform a few run-time duties and makes it possible to catch problems that would have resulted in exceptions or crashes or undefined behavior otherwise. If a C# programmer consciously uses the features offered by this generous language, I believe the resulting program will be less buggy.

The more I learn C#, the more I like it.

PS: This article explains well when to use new and when to use override keywords.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: