Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an instance of an object in c#

Tags:

c#

asp.net

Greeting for the day!

I have a question in my mind and looking for answer from some days. If my understanding is correct then only diff between Instance and object is :-

instance means just creating a reference(copy) .

object :means when memory location is associated with the object( is a runtime entity of the class) by using the new operator

Now i want to know how to create an instance of an object. Please give explanation with sample code

Any help will be appreciated. Thanks

like image 219
Amit Soni Avatar asked Aug 17 '11 09:08

Amit Soni


People also ask

How do I create an instance of a class in Objective C?

You can create a class reference with the following code: Class M = [NSMutableString class]; // NSMutableString (for example). You can then call methods on that saved class with code like this: [M string];

What creates an instance of an object?

Variables declared inside a class but outside the scope of any blocks, constructors, or methods are known as instance variables in Java. To create instance variables, an object must be instantiated and accessible to all blocks, constructors and methods in that class.

What is an instance of an object?

Instance : Its a variable which is used to hold memory address of the object. A very basic analytical example. Class House --> Blueprint of the house. But you can't live in the blue print. You need a physical House which is the instance of the class to live in.

Can be used to create a new instance of an object?

Answer. To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods. For example, to create a new StringBuffer object, we would use the new keyword in the following way.


3 Answers

By your explanation it's not called an instance, but a reference of an object. An instance of a class is called an object. I think your question is: "What is the difference of an object and a reference variable?" I'll try to explain it with some examples:

Foo f;

I just declared a reference variable. This is not an object but only a reference that refers to an object.

f = new Foo();

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

Foo otherFoo;

Now I declare another reference variable.

otherFoo = f;

What we have here now is having ONE object in the memory but TWO reference variables refering to 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.

I hope that explains you everything. :)

like image 146
Bosak Avatar answered Oct 17 '22 19:10

Bosak


You don't create "an instance of an object", you create an instance of a class (or struct). An object is an instance of a class.

If you do:

Foo f = new Foo();

You create an instance of the Foo class.

like image 25
Thomas Levesque Avatar answered Oct 17 '22 19:10

Thomas Levesque


In C# 9.0 there is a new way to initialize a class by Target-typed new expressions.

You can initialize the class like this:

Foo f = new();

Note, f is a reference to the class Foo.

like image 21
Misha Zaslavsky Avatar answered Oct 17 '22 19:10

Misha Zaslavsky