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";
}
}