Reputation: 1198
Hey guys, just starting out with C#. I had a few doubts, would really appreciate it if anyone could help me out here
animal dog;
dog = new animal();
what's the difference between the above two lines ?
namespace proj1
{
public class form1:form
{
guy bob;
public form1()
{
initialize component();
bob = new guy();
bob.name = "bob";
}
}
When the Form1 class is instantiated, the Constructor form1() is the first thing to run.
But how can bob = new guy() be executed before Guy bob which is in the data member declaration of the class ??
Upvotes: 2
Views: 229
Reputation: 14162
Question one
The first line is declaring a new variable called dog of type animal. That is you are saying "I have a new variable which I can refer to by using its name, 'dog'. In this variable I can only put things of type 'animal' in it."
It doesn't actually do anything at runtime, it is simply declaring your intent to the compiler. You're telling it you want a new variable, how you are going to refer to it (using the name 'dog'), and what sort of values you can assign to it (things of type 'animal')
The second line is the bit that actually does something. It creates a new object of type animal, then assigns that object to the variable you had declared previously, meaning you can use the variable elsewhere to refer to the object you created.
You can only assign things to variables you have told the compiler about (i.e. declared its name and type). This allows the compiler to check all sorts of things for you and provides some level of what is called type safety. [Other languages don't have these restrictions, which makes them more concise and flexible, but at the risk of making it easier to introduce subtle bugs]
You can combine the two steps -- the variable declaration and the assignment -- like so:
animal dog = new animal();
This declares to the compiler a new variable called 'dog', of type 'animal'. At runtime it creates a new object of type animal and assigns it to the variable we'd declared called 'dog'.
Upvotes: 0
Reputation: 49978
Q1: The first line declares the variable, the second assigns a new object instance to it
Q2: I would take a look at the Instance Variable initializers section from Jon Skeet's page to better understand what is going on.
Upvotes: 0
Reputation: 74250
1 - the first line declares the variable, the second assigns a new object instance to it
2 - "Guy bob" is just a declaration and does not get "executed"
Upvotes: 3
Reputation: 351456
Question 1:
animal dog;
dog = new animal();
In the first line of this example you are declaring a variable of type animal
. This means that this variable name and its type will be emitted by the C# compiler into the assembly as part of the IL for the stack of whatever method you are currently in.
The second line is the interesting part. On the second line you are creating a new instance of the animal
type and assigning the reference to that instance to the dog
variable that you created on the previous line. This code is also emitted to the assembly but this represents code that will run at execution time when this line is invoked.
Question 2:
public class form1 : form
{
guy bob;
public form1()
{
initialize component();
bob = new guy();
bob.name = "bob";
}
}
This example is a bit more interesting because you are dealing with bigger concepts than in the first example. In this example you have declared a private field in the class form1
- this is handled differently than simply declaring a variable like in your first question.
A field is part of the state of the type you are currently in. In other words, by declaring this variable in the scope of the current type you are indicating that this variable is a field and is therefore part of the blueprint of the type. This means that this field is declared for all instances of this type.
This means that the declaration of guy bob;
actually is defined far before you ever assign any references to the bob
field. It is actually defined at the time of compilation since the compiler will emit this field into the assembly as a field and the assembly's resulting metadata will reflect this field as well. Since this field is defined at compilation time you can see why you are able to assign a reference to it because the code that assigns the reference to the field does not get executed until the code is run.
Upvotes: 2
Reputation: 15441
Question One: It is declaring the variable (dog), which is also called a reference variable because, new animal() Provides/Allocate memory space, while specifying the type of object this memory space should contain. So by doing this (dog = new animal()) you are providing a reference to the memory location through the variable dog.
Question Two: At compile time you program know that the object (Class) has a reference variable bob that must contain an object type of "gug". So it is more like the architecture of a build yet to be built. guy bob is never executed. This is what is executed "bob = new guy();".
Upvotes: 0
Reputation: 25200
Ram has it for Q1. I'd go further to say
Animal rover = new Animal()
as rover is a particular instance (realization) of an animal.
For Q2, there's two distinct parts to running a C# program - first, the program is compiled, then the compiled program is run.
During compilation all the object declarations, method definitions and whatnot are scanned and written out to Microsoft Intermediate Language, MSIL. The ordering of these doesn't make any difference for these declarations, it's only the behaviour of your program, the bits in the method bodies, where ordering is important.
Put another way, there's two sets of things going on here: "Given x and y and z, and that Bob will be an object of type Guy..." and "... go do this, this and this, make Bob an object of type Guy, and so on."
Taking this further it's quite possible to have all your object declarations in one file and the methods in another. (These partial classes are used by the designers in Visual Studio.)
Upvotes: 1
Reputation: 11626
Q1: animal is the class and dog is declared as an instance of animal. If I were to give you an anology, think class as a blueprint of a building and the object is the actual realization of that blueprint i.e. a house. dog = new animal() actually assigns it the required memory space. If you try accessing dog's properties/instance methods/public variables without doing a "dog = new animal()", you will get a runtime error "object reference not set to instance of the object"
Q2: same goes here. If you are coming from a C background, where the method is placed in the code maters (method declaration, if I remember it right) but not in C#.
Upvotes: 0
Reputation: 234354
Guy bob
just says "bob is a variable that holds values of type Guy". bob = new Guy()
says "The variable bob now holds a new value of type Guy"
Upvotes: 2
Reputation: 12082
In the first example, first line you haven't actually instantiated your object, merely just declared it. The second line does instantiate (new()
) the object by calling it's constructor.
Your second example is a similar thing, form1()
is your form class' constructor and will instantiate your private member field bob
.
Read up on constructors I'd say. Start with this Wikipedia article.
Upvotes: 0
Reputation: 17321
Question #1
animal dog; //declares that the variable will be used
dog = new animal(); //the actual object is made in memory and linked to the variable name
Question #2
You can't assign a value to a variable before it is declared. The best you can do is do it all in one line.
Guy bob = new guy() ;
Upvotes: 0
Reputation: 1635
Q1: First is a declaration (it doesn't "do" anything at runtime).
Q2: guy bob again is just a declaration. It is not executed, it merely tells the compiler what the object should contain. The object is not constructed until new guy().
Upvotes: 2