Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im wondering how this code is working:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_Practicing
{
    class Parent
    {
        public string name;
        
        public Parent()
        {
            Console.WriteLine("Hello from the parent constructor");
        }
    }

    class Child : Parent
    {
        public Child()
        {
            Console.WriteLine("Hello from the child constructor"); 
            name = "Bob";
            Console.WriteLine("My name is: {0}", name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Child c = new Child();
        }
    }
}


Also the same is happening if i use this.name (but how its working isn't the keyword "this" refering to the members ONLY in the (in my example the) Child class ???)


But this code wont work:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_Practicing
{
    class Parent
    {
        public string name;
        
        public Parent()
        {
            Console.WriteLine("Hello from the Parent constructor");
        }
    }

    class Child : Parent
    {
        name = "Bob"; 
    }

    class Program
    {
        static void Main(string[] args)
        {
            Child c = new Child();
        }
    }
}


And here the question is WHY its not working ?

What I have tried:

Asking question here in CodeProject.com because I got very confused when it happened to me and I cant understand what exactly is happening with this code.
Posted
Updated 25-Jun-17 23:16pm

1 solution

The first example works because Child is derived from Parent so it has all of the properties of Parent, including the "name" variable. The second example doesn't work because it is invalid c#. A class can directly contain variable declarations or functions, it can't directly contain code, the code needs to be "in" something (in the working example it is in the constructor). This would work though

class Child : Parent
{
    public void MyFunction()
    {
        name = "Bob";
    }
}
 
Share this answer
 
Comments
The_Unknown_Member 26-Jun-17 6:33am    
But if initialize the field "name" in the Parent class it will work. So why there is problem when i want to initialize the field "name" from the derived class ? I just cant understand
F-ES Sitecore 26-Jun-17 6:43am    
In the parent class you are defining the variable

public string name;

that is legal. In the child class you were setting its value in code

name = "Bob";

which isn't legal syntax. It isn't in a function so when would that code run? If it is to run when the class is created then it should be in the constructor.
The_Unknown_Member 26-Jun-17 8:08am    
But If I declare and initialize a field in one statement its working. For example:

public string name = "Bob"; // Working

but this wont work:

public string name; // Declaring the field
name = "Bob"; // Initializing the field in the same class
F-ES Sitecore 26-Jun-17 8:33am    
That's because the compiler recognises this

public string name = "Bob";

and does something special with it. It defines the variable and it also moves the "=Bob" bit to a special initialisation event\constructor so that it knows how to default all the values. In order to do this you need to stick to that specific syntax. To the compiler, this code here

public string name;
name = "Bob";

is no different from

public string name;
for(int i; i < 10; i++){}

You are defining a variable and then writing code. The compiler can't be bothered working out that a for loop is invalid but "name=Bob" is a declaration so should be moved to the constructor, so instead you have to stick to the valid syntax the language provides.
The_Unknown_Member 27-Jun-17 4:06am    
I understood it. Thanks for explaining it to me! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900