Data Types in C#

C# provides us with an Unified Type System. This means that all data types in C#, be it a reference type of value type, are derived from just one class, the object class. It simply means that the object class is the ultimate base class for all data types. This is actually great news. It means that literally everything in C# is an object since all types are derived from the object class.

Consider the following example:

Example 14:

Example14

Observe the line 7 in Example 14. It is something we have not come across in any programming language. After all 9 is a number how can it have a method? But no! This is the C# realm, where everything is an object! Even the number 9! It has methods of which we have shown one, the ToString() method. This method converts the number to a string. The code will execute without any errors and will store the number 9 as a string in the variable CheckThis as expected. Astounding isn’t it? One more exciting thing about C# is that since all types are derived from a common class (namely object) they all have similar characteristics.

Static members

Sometimes it is necessary to define members in a class that are not associated with any particular object other than the class itself. This means that there will be only one instance of this method/field no matter how many objects of the class exist.

Static members can be defined in C# by using the keyword static, as in Example 15.

Example 15:

Example15

Leave a comment