Click here to Skip to main content
15,849,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
namespace sa
{
    public class Text
        {
            public class B
            {
            }
            public delegate void f(int a,int b);
            public void t(int x, int y)
            {
                Console.WriteLine((x+y));
            }
            public static void Main()
            { 
                B b=new B();
                Text text=new Text();
                text.t(2,3);
            }
        }
}

t() should be used by the text object.
Why can the nested class B be used directly?
Is class B considered static by the compiler?
Posted
Updated 26-May-10 6:43am
v6
Comments
Johnny J. 26-May-10 4:44am    
Added PRE tags to make your code more readable...

shilf wrote:
is class B static by compiler?

Class B is not static. You would not be able to do
B b=new B(); if this was the case.


For different samples on nested classes, have a look here.
 
Share this answer
 
v3
Comments
shilf 26-May-10 8:17am    
A Tutorial on Nested Classes in C#
:http://www.codeproject.com/KB/cs/nested_csclasses.aspx#_comments
it is perfect
thank you,Answer 2
thanks for all helper
shilf 26-May-10 9:07am    
is Class like Namespace?but its' scope is smaller than Namespace
shilf 26-May-10 20:33pm    
I am a student in china,the book and website about computer programming are very little and poor.who can introduce me some website about computer in english
Your main method is defined inside the class Text. So the B class is directly visible to it.

Try to move Main() into another class and it will be no more visible to it directly.
 
Share this answer
 
Comments
shilf 26-May-10 4:43am    
I know Answer 1 is right. In main() I also write: Text.B b=new Text.B(); it is also right ;these two method is both right! why we can write: B b=new B();
shilf 26-May-10 4:48am    
is the nested class static by compiler?

who can tell me the internal organization?
voloda2 26-May-10 4:58am    
Not sure if I understand what are you asking about.

The nested class V is a standard class, which can be instantiated based on its vibility modifiers.
It has only one speciality - if you have an instance of the top class A, you can access its private members from the V's code.


class A
{
private int i;
public A()
{

}
public class V
{
int ii;

public V()
{
A a = new A();
a.i = 10;
}
}
}


It's more like an additional namespace.
shilf 26-May-10 5:07am    
Voloda2: i is private in Class A; i can not be used out of class A ;I think in Class V {a.i is wrong}
voloda2 26-May-10 5:13am    
That's exactly what am I writting about - it's a speciality of class V - it can access private members of the top class A.

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