[Solved] New object initialization in C#


yes you can initialize an object without any parameters,
let me explain simply,
an object is just a copy from a class, it has a copy of all the public methods and fields inside this class.
We use objects to access members of a class
so let’s assume you have two classes X and Y
and you want to be able to use the non-static public members of class X in class Y
then in class Y you can instantiate an object from class X

X myObjectName = new X();

So what about the constructor part (the parenthesis)
this is just for you to initialize fields defined in class X constructor, in other words you might want to access the public members in class X but you want them to have certain values before you access them, so in class X you should make a constructor, which basically says whatever object instantiated from my class should initialize the following fields, for example in class X you could have this constructor

public X (int num) {
this.num = num;
}

This basically means that when you instantiate an object from class X you will be required to also pass an integer to initialize field num in class X. Yet you have to know that whatever value you will initialize num with, this value is just a copy that exists in your object but not other objects and not the class itself
for example you can make the following two objects from class X in class Y

X object_1 = new X(5);
X object_2 = new X(12);

this doesn’t mean that you change the value of num field in class X but rather means that you made two objects that are a copy of that class and each copy has a different value of num.
So the reason why you can make an object without initializing any fields, is if you for example want to access the class methods only or maybe you want the default values for the fields. btw you can overload constructors, which means you can multiple constructors that one for example that requires no arguments, so you can use this one when you want to instantiate an object without any parameters, an one that accepts an integer and you use this one when you want to initialize the value of num field when you instantiate the object example

public X () {

}

and the other is

public X(int num) {
this.num = num
}

Then in class Y you can make this

X object_1 = new X();
X object_2 = new X(15);

I hope i gave you a good explanation

solved New object initialization in C#