Hey guys, just starting out with C#. I had a few doubts, would really appreciate it if anyone could help me out here

Question #1

animal dog;
dog = new animal();

what's the difference between the above two lines ?

Question #2

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 ??

Hi reddevilz! Welcome on Daniweb!

Question #1:
The first line says that the variable(or field in C# parlance) dog is of type animal. Almost the same as saying int i;.
Now the compiler infers that the int type is a struct and allocates space for it on what is called a stack. (A pile of dishes a compiler keeps in his kitchen...;) ) So you can instantly say: i = 42;
You cannot do that with your dog variable. You cannot immediately say dog.Name = "Blacky"; You first have to bring your dog alive by executing the second line of your code. The new keyword allocates memory space for an animal type somewhere in RAM(an area that is called the heap) And now you can say dog.Name = "Blacky"; and use your dog variable with the fields and methods defined in your animal class.
You should now be able to figure out your second question, with what I told you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.