Why do we create new instances of variables?

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

I have covered a few books on C# programming, but I have never figured out why we make new instances or data types of a variable.

Look at this:

C#:
double[] arrayName = new double[] {10, 5, -6, 8};

Why do we make the array variable equal to a new variable again?

why can't we just do:

C#:
double[] arrayname = {10, 5, -6, 8};

What makes them so different?

Thanks
 
Firstly, you need to understand the difference between a variable and an object. A variable is a named location to store data and an object is data. This code:
Thing stuff;

declares a variable named 'stuff' of type 'Thing' but it doesn't assign anything to that variable, so the variable is 'null', i.e. has no value. This code:
Thing stuff = new Thing();

also declares a variable named 'stuff' of type 'Thing' and it also creates a new object of type 'Thing' and assigns it to the variable. It's not creating a new instance of a variable; it's creating a new instance of a type and assigning that object to a variable.

Arrays are a thing that has direct support built into the language, which means there is dedicated syntax for deal with arrays. Just like the first code snippet above, this code declares a variable but does not assign it a value:
double[] stuff;

That 'stuff' variable can refer to a 'double' array but has had no object assigned to it so is 'null'. This code:
double[] stuff = new double[] {10, 5, -6, 8};

does just what the second code snippet above does, i.e. declares a variable, creates an object and assigns the object to the variable.

C# syntax has evolved over the years and sometimes there are multiple ways to do the same thing. The code that you have provided is just such a case. In the first snippet, you're explicitly stating that the array being created is type 'double' while, in the second, that is implied by the usage. Note that you can only omit the 'new' keyword if you initialise the variable where it's declared. If you were to split that into two lines then the first snippet would work like this:
double[] arrayName;

arrayName = new double[] {10, 5, -6, 8};

but the second snippet like this:
double[] arrayname;

arrayname = {10, 5, -6, 8};

would fail with a syntax error. Note that this is also valid:
double[] arrayName = new [] {10, 5, -6, 8};

because, again, the type of the array can be inferred. On the other hand, this:
double[] arrayName;

arrayName = new [] {10, 5, -6, 8};

would fail because the values in the array are actually type 'int' and not type 'double'. In that case, to omit the type, you must actually use 'double' values:
double[] arrayName;

arrayName = new [] {10.0, 5.0, -6.0, 8.0};

So, in short, the second code snippet is functionally equivalent to the first but would not have been valid in some earlier versions of C# and also the first snippet can be broken into multiple lines and still work while the second cannot.
 
Firstly, you need to understand the difference between a variable and an object. A variable is a named location to store data and an object is data. This code:
Thing stuff;

declares a variable named 'stuff' of type 'Thing' but it doesn't assign anything to that variable, so the variable is 'null', i.e. has no value. This code:
Thing stuff = new Thing();

also declares a variable named 'stuff' of type 'Thing' and it also creates a new object of type 'Thing' and assigns it to the variable. It's not creating a new instance of a variable; it's creating a new instance of a type and assigning that object to a variable.

Arrays are a thing that has direct support built into the language, which means there is dedicated syntax for deal with arrays. Just like the first code snippet above, this code declares a variable but does not assign it a value:
double[] stuff;

That 'stuff' variable can refer to a 'double' array but has had no object assigned to it so is 'null'. This code:
double[] stuff = new double[] {10, 5, -6, 8};

does just what the second code snippet above does, i.e. declares a variable, creates an object and assigns the object to the variable.

C# syntax has evolved over the years and sometimes there are multiple ways to do the same thing. The code that you have provided is just such a case. In the first snippet, you're explicitly stating that the array being created is type 'double' while, in the second, that is implied by the usage. Note that you can only omit the 'new' keyword if you initialise the variable where it's declared. If you were to split that into two lines then the first snippet would work like this:
double[] arrayName;

arrayName = new double[] {10, 5, -6, 8};

but the second snippet like this:
double[] arrayname;

arrayname = {10, 5, -6, 8};

would fail with a syntax error. Note that this is also valid:
double[] arrayName = new [] {10, 5, -6, 8};

because, again, the type of the array can be inferred. On the other hand, this:
double[] arrayName;

arrayName = new [] {10, 5, -6, 8};

would fail because the values in the array are actually type 'int' and not type 'double'. In that case, to omit the type, you must actually use 'double' values:
double[] arrayName;
arrayName = new [] {10.0, 5.0, -6.0, 8.0};

So, in short, the second code snippet is functionally equivalent to the first but would not have been valid in some earlier versions of C# and also the first snippet can be broken into multiple lines and still work while the second cannot.

Hey, thanks for the reply.

I now understand how we create new objects of the variable. But why do we need to create objects of them?

I am baffled on this topic here.
 
We don't "create new objects of the variable". That doesn't really mean anything. OOP is supposed to mirror the real world. How would you expect the real world to work with no objects. You could say "I'm going to drive my car" but what use would that be if you didn't actually have a car to drive? Similarly, how can you store numbers or other data in an array fi you don't actually create an array? Declaring an array variable is simply saying that you have a place that an array can be stored. If you don't actually create an array to store there, it's of no value.
 
We don't "create new objects of the variable". That doesn't really mean anything. OOP is supposed to mirror the real world. How would you expect the real world to work with no objects. You could say "I'm going to drive my car" but what use would that be if you didn't actually have a car to drive? Similarly, how can you store numbers or other data in an array fi you don't actually create an array? Declaring an array variable is simply saying that you have a place that an array can be stored. If you don't actually create an array to store there, it's of no value.

Boom! I get it now.

Good job and thank you for explaining that, it makes more sense.
 
Back
Top Bottom