Quantcast
Channel: CodePattern.net
Viewing all articles
Browse latest Browse all 70

Super Funda of OOPs in C# - Part 1

$
0
0

C# Language is an Object Oriented Programming model language from Microsoft which is evolving with day by day. It is a leading programming language in Microsoft stacks. As per the standard guidelines of this language, it behaves in following manner while making a type(class) ready to work -

1. Default initialization:

Variables are initializes with default values if you don't initialize them explicitly while declaration. As per the MSDN,

"All the variable initializers for a class are implicitly run directly before the invocation of whichever base class constructor is invoked".

Instance & static variables can be initialized at the point of its declaration.

  • Instance variables - Initialization is happen just before the call of constructor.
  • Static variables - Initialization is happen just before the first usage call of the member or just before the first instance creation of that class.

 

2. Default inheritance

If a class is not inheriting to any base class then, .net implicitly provide an inheritance to its Object class. It is interesting, no? Object plays mother roles for delegate, array, struct, enum or nullable value type.(every non-pointer type in C# is convertible to object.) Now, you would be thinking, what if my class is derived one? In this case too we indirectly have Object class derived through multilevel inheritance.


public class MyBaseClass

{

is the same as-

public class MyBaseClass : Object 

{

}
Now, you can understand the mystery of providing default constructor, right? Yes, it is the Object class's parameter-less constructor.

http://referencesource.microsoft.com/#mscorlib/system/object.cs,d9262ceecc1719ab

 

3. Default constructor:

A default constructor is provided for class's object creation if no constructor is explicitly given by user. a special code block which is guaranteed to be executed while the creation of variables to make a class instance workable.
A base class constructor is automatically called. C# Language provides options to explicitly called base class constructors in which we can specify which base constructor is to be called by passing appropriate parameters. This constructor chaining is provided by language to avoid duplicate codes.
Static Constructor- It invoked just before the-

(1) First invocation of static method or (2) First time an instance of that class is created.

 

@AnilAwadh


Viewing all articles
Browse latest Browse all 70

Latest Images

Trending Articles



Latest Images