08-DotNet UserDefinedDataTypeMembers
08-DotNet UserDefinedDataTypeMembers
Constructors Overview
Whenever a class or struct is instantiated using “new” keyword, its constructor is called implicitly.
Constructors enable the programmer to set default values and limit instantiation.
Default / Parameterless Constructors :
If you do not provide a constructor for your class, C# will create one by default that instantiates the
object and sets member variables to the default values shown in next slide.
C# VB.Net
Usage
See Also
C# VB.Net
Usage
Constructors
Instance Constructors
Instance constructors are used to create and initialize any instance member variables when
you use the new expression to create an object of a class.
C# VB.Net
Usage
Constructors
Static Constructors
A static constructor is used to initialize any static data, or to perform a particular action that
needs to be performed once only. It is called implicitly for the first instance; it’ll not be called for the
subsequent instances. Note: Static members cannot access instance members.
C# VB.Net
Constructors
Private Constructors
A private constructor is a special instance constructor. It is generally used in classes that
contain static members only. It is used in Singleton Pattern, Helper class implementation.
C# VB.Net
Usage
Constructors
Copy Constructors
Unlike some languages, C# does not provide a copy constructor. If you create a new object
and want to copy the values from an existing object, you have to write the appropriate method
yourself.
C# VB.Net
Usage
Destructors
Destructors
Destructors are used to destruct instances of classes. This is called by the GC during reclaiming the
memory.
Destructors cannot be defined in structs. They are only used with classes.
A class can only have one destructor.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.
C# VB.Net
Properties
Overview
A property is a member that provides a flexible mechanism to read, write, or compute the value of a
private field. Properties can be used as if they are public data members, but they are actually special
methods called accessors. This enables data to be accessed easily and still helps promote the safety
and flexibility of methods.
C# VB.Net
Usage