How to create an instance of an object in C #

Greetings for the day!

I have a question in my opinion and a search for an answer from several days. If my understanding is correct, then only the difference between the instance and the object: -

an instance simply means creating a link (copy).

object: means the memory location is associated with an object (is a class runtime object) using the new operator

Now I want to know how to instantiate an object. Please provide an explanation with a sample code.

Any help would be appreciated. Thanks

+4
source share
5 answers

According to your explanation, it is not called an instance, but an object reference. An instance of a class is called an object. I think your question is: "What is the difference between an object and a reference variable?" I will try to explain this with a few examples:

Foo f; 

I just declared a reference variable. This is not an object, but only a link related to the object.

 f = new Foo(); 

Now I created a new object and assigned it to the reference variable f , so every time I do something with f , I refer to the Foo object. For example, when I call f.Name = "MyFoo"; I am referring to a foo object.

 Foo otherFoo; 

Now I am declaring another reference variable.

 otherFoo = f; 

Now we have ONE object in memory, but TWO strong> referenced variables that reference the same object.

 f.IsFoo = true; bool isotherFooFoo = otherFoo.IsFoo; 

This last line will return true because we changed the IsFoo property to true and f and otherFoo reffer to the same object.

Hope this will explain everything to you. :)

+5
source

You do not create an "instance of an object", you create an instance of a class (or structure). An object is an instance of a class.

If you do:

 Foo f = new Foo(); 

An instance of the Foo class is created.

+4
source

We have class ABC

 Class ABC { string name=""; public ABC() { this.name = "A1"; } public ABC(name) { this.name = name; } } 

An instance of the class can be created as:

 ABC a1 = new ABC(); 

or

 ABC a1 = new ABC("James"); 
0
source

In the phrase “object is an instance of the class” the word “instance” does not actually have a technical meaning that differs from the word “object”, it is just a way of defining in English what the word “object” means. The value "instance" actually means the same as the value "object". We can break it down as follows:

 an object is an instance of a class an object = instance of a class an object = instance 
0
source

You create an instance of a class, not an object.

-1
source

All Articles