CONSTRUCTORS
PUBLISHED: 9/27/2023
Last updated
PUBLISHED: 9/27/2023
Last updated
--> special methods in C# that are automatically called when an object of a class is created to initialize all the class data members
--> A constructor without parameters
--> A constructor having at least one parameter
--> A parameterized constructor that contains a parameter of the same class type
--> When a constructor is created with the private specifier
--> When a constructor is created with the static specifier
A constructor is a special method that is used to initialize objects.
--> Can not have a return type, even void --> The constructor is called when an object of a class is created --> Can be used to set initial values for fields
A constructor without parameters is known as a Default Constructor.
Default Constructors... --> has every instance of the class to be initialized to the same values --> if no value is specified, initializes all numeric fields to zero --> if no value is specified, initializes all string and object fields to null
LINE 3 & 4 --> Create a field
LINE 6 --> Create a constructor for class "Car"
LINE 14 --> Create a object of the constructor "Car" | This will call the constructor
LINE 15 --> Print the value of the model for Ford | OUTPUT will be "Mustang"
LINE 16 --> Print the value of the year for Ford | OUTPUT will be "0"
A constructor having at least one parameter is known as a parameterized constructor. By the constructor having parameters, it can initialize each instance of the class to different values.
LINE 3 & 4 --> Create a field
LINE 6 --> Create a constructor for class "Car" that holds 2 parameters
LINE 14 & 15--> Create a object of the constructor "Car" passing values through the parameters | This will call the constructor
LINE 16 --> Print the value of the model and year for Ford | OUTPUT will be "Mustang + 1964"
LINE 16 --> Print the value of the model and year for Chevy | OUTPUT will be "Cruz + 2013"
A parameterized constructor that contains a parameter of the same class type is known as a copy constructor. Useful whenever we want to initialize a new instance to the values of an existing instance.
Use copy constructors to copy one object's data into another object.
The objective of the following code is to create the player's character and, through the use of a copy constructor, build a villain character and modify its attributes.
LINES 3-5 --> Create fields that will be used to define a character
LINE 7 --> Create a constructor to form a character | known as the Original Constructor
LINE 14 --> Create a constructor that will copy the character | known as the Copy Constructor
LINE 21 --> Create a method that will display the stats of a given character
LINE 30 --> Creates a playerCharacter using the original Constructor
LINE 32 --> Creates a villainCharacter based off the playerCharacter by using the Copy Constructors
LINE 34 & 35 --> Modifys the Name and AttackDamage of the villainCharacter
LINE 37 & 38 --> Displays Player stats
OUTPUT Player Stats are: Name: Hero Health: 100 AttackDamage: 20
LINE 39 & 40 --> Displays Villain stats
OUTPUT Villain Stats are: Name: Villain Health: 100 AttackDamage: 30
When a constructor is created with the private specifier, it is known as a PRIVATE CONSTRUCTOR. It is used to restrict the creation of instances of a class from outside the class itself.
When a constructor is created with the static specifier, it is known as a STATIC CONSTRUCTOR. It is used to initialize static members of a class before any static member is accessed or any static method is called. Static constructors are automatically called by the runtime, and are useful for performing one-time initialization tasks for a class.
INITIALIZATION OF STATIC MEMBERS --> Static constructors are primarily used for initializing static members of a class. These members are shared among all instances of the class and are not tied to any specific instance.
AUTOMATIC INVOCATION --> Static constructors are automatically invoked by the .NET runtime the first time a static member is accessed or a static method is called on a class.
PARAMETERLESS --> Static Constructors do not take any parameters and can not pass any arguments when calling them.
SINGLE EXECUTION --> A static constructor is guaranteed to run only once per application domain, regardless of how many instances of the class are created or how many times static members are accessed.
Private Constructors are often used in the .