user2189184
user2189184

Reputation:

Difference between variable and object

Well, There are two statements occurred repeatedly in tutorials from where I'm learning C#.net

DataRow dr;

What does this mean. I assume it is a variable of DataRow class.

DataRow dr = new DataRow();

What does this mean. I assume it is an object of DataRow Class.

But still I left with an ambiguity that In which case I've to use 1st case Or 2nd Case. I'm quite confused now.

I assumed that this question has been repeated in Java forum. but I didn't get it as I don't have any background of Java. so before marking it repeat consider this.

Upvotes: 3

Views: 3662

Answers (5)

Tamir Vered
Tamir Vered

Reputation: 10287

Creating a variable has 2 parts, Declaration and Assignment.

In the Declaration part, you state that the variable exists and you state its type:

DataRow dr; // Create's a variable of type DataRow with a value of null.

The Assignment takes a value and point the variable to it:

dr = new DataRow(); // Creates a new DataRow and points dr variable to it.

You can do them both in the same line like that:

DataRow dr = new DataRow();

The result is the same as if you would separate those into the 2 lines above and the type of the variable is in both cases DataRow.

The object inside the variable however can be of an inherited type:

class MyDataRow : DataRow
{
}

DataRow dr = new MyDataRow(); //Creates a new object of type MyDataRow and points dr to it.

Managed objects must be held by a variable otherwise they are Garbage Collected.

Upvotes: 6

Baskar Rao
Baskar Rao

Reputation: 470

In C# a DataRow can never be instantiated. It can always be used either as

DataTable dt=new DataTable(); DataRow dr=dt.NewRow();

This above statement creates a new row for the the table.

   DataRow dr=dt.Rows[0];

This above statement gets the first row from the datatable.

The below statement will never execute when compiled.

  DataRow dr=new DataRow();

Error Screenshot on compilation

Upvotes: 0

Wormbo
Wormbo

Reputation: 4992

Variables in C#, like in most other languages, are places to store something in. Think of them as specially shaped drawers that you can fit exactly one thing in. Things to fit such a drawer are values.

C# essentially has two basic kinds of values: "value type" values and object references. (Let's ignore "pointers" for now.)

A "value type" is anything that C# defines as a struct or enum, which includes basic types such as int, bool, char or double. These values are directly stored in a variable, and if you use the variable in any way, the value is copied to where it's used (e.g. assigned to a new variable, or passed as argument to some method or operator).

All types in C# that not a value type are "reference types", which includes arrays, strings and most other classes. Instances of a reference type are not stored in variables, but reside in a special memory area called "heap". When you "assign and object to a variable", you actually only put a reference to that object into the variable. When accessing the variable, C# follows the reference to allow access to the object's members. In other words, the "value" of a reference type is just the reference to the object. So, when you pass the variable's content to some operator or method or assign it to some other variable, you only copy the reference, not the object behind it.

Upvotes: 2

user2404597
user2404597

Reputation: 508

DataRow dr;

This basiclay tells dr is a variable of "type" DataRow. it does NOT create the DataRow Object itself.

**DataRow dr = new DataRow();**

This actually creates the object and stores it into "dr" variable.

You could write it like this...

DataRow dr;
dr = new DataRow();

notice at second line, I already knew "dr" is of type DataRow.

if you want to call any function of Datarow, then first you MUST create a instance of it..instances are created by using the "new" keyword.

Upvotes: 0

Spaceman
Spaceman

Reputation: 1339

DataRow dr; 

Is creating a variable with the type of DataRow but it will be null.

DataRow dr = new DataRow(); 

Is creating a variable and assigning it with a new value which in this case is an instance of a DataRow.

Upvotes: 0

Related Questions