What will be the output of this code?

using System;

class A
{
class B
{
public string str = "welcome";
}
}

class Demo
{
static void Main()
{
B Obj = new B();
Console.WriteLine(Obj.str);
}
}

 Posted by Ddd on 2/15/2011 | Category: .NET Framework Interview questions | Views: 3375 | Points: 40
Answer:

Answer:

The compiler will generate an error

The Type or Namespace 'B' could not be found.

It is because since B is the inner class, it is now private.

Solution:

1)we have to change the access modifier of B to internal/public

2)we have to give the full path of class B when creating the object

i.e. A.B obj=new A.B();
Then we can see the value of the field str in the output.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response