Constructor understanding CSharp dotnet programming with dotnet core 7

preview_player
Показать описание
C# Constructor understanding CSharp dotnet programming with dotnet core,
Visual studio 2022 #csharp #programming #objectorientedprogramming #constructor #dotnet #dotnetcore

Points:
Constructors
• Constructor is a method with no return type to initialize the variable of that class.
o Non-static constructor: also called instance constructor
 to instantiate the default values to the variable and parameters defined in the class.
 it allows to create object of the class based on access modifiers.
 Allow to modify the values of the parameter on class object call.
 Optional access specifiers.
 It can be parameter less and parameterized.
 Multiple constructors can be defined with different number or type of parameters including on parameterless constructor.
o Static constructors: static constructors are called automatically, immediately before any static fields are accessed.
 Static constructor can be defined inside class.
 Used to initialize static members
 Parameter less
 Only one static constructor can be defined
 C# compiler initialize default value based on type to static fields if we do not define explicitly in static constructor.
 Can not be inherited or overloaded.
• Constructor without any parameter is called parameterless constructor.
• If we do not define constructor explicitly then C# compiler calls public parameterless constructor to instantiate class.
• To call constructor use new or base or this keyword.
• To restrict the class from being instantiate from outside class, we can define private constructor.
• If we define constructor explicitly without any access specifiers then by default it will be private constructor.
• If we define only private constructor then we cannot instantiate class from outside the class but within class we can create object.
• It is used when class have static members only.
• Static constructor is used to initialize the static data members.
• If you define the variable as static readonly then it is allowed to initialize its value inside static constructor only.
• We should initialize only that data which need to be initialized only once.
• Static class automatically calls the static constructor.
• Non-static class can have static and non-static constructor.
• Static class can have only static constructor we can not call non-static constructor.
Рекомендации по теме
visit shbcf.ru