Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
namespace sa
{
	public class A
	{
		public static void f()
		{
		}
		public void g()
		{
		}
		static void Main()
		{
			f();
			A a=new A();
			a.g();
		}
	}

	class OuterClass
	{
		public static int y = 100;
		public int x=23;
		public class NestedClass
		{
			int i=y;
                        public void t()
                        {
			         OuterClass b=new OuterClass();
			         int j=b.x;
                        }
		}
	}
}


why f() within class A can be used directly in Main(),but we must create object a to use g()?

the same;why y within class OuterClass can be used directly in nested NestedClass,but we must create object b to use x?

the two conditions are similar, who can tell me the internal mechanism?thank you
Posted
Updated 26-May-10 4:54am
v3
Comments
Johnny J. 26-May-10 9:43am    
Added PRE tags (again)

Stop posting homework questions.

You asked a very similar thing a little earlier, and you failed to understand that.

Go back to your teacher and bother him about it, otherwise you will alienate and, worse, bore people to death, with a thousand questions whose answers are meaningless.
 
Share this answer
 
v2
Comments
shilf 26-May-10 9:58am    
Dalek Dave:thanks for your suggestion.
but I think the question is very complicated.it is not my homework.
my question is refer to C# internal mechanism.
The reasons for all of these, and to your previous question, are because of the 'scope' or 'visibility' of the objects/methods/members in question.

Try searching on something like c# method scope and so on.

An example[^] for free.

You will get far more by doing that than you will from continually asking here. A site like Code Project is not really set up to give tutorials.
 
Share this answer
 
Comments
shilf 26-May-10 10:26am    
Henry Minute:Abhinav S,A Tutorial on Nested Classes in C#: http://www.codeproject.com/KB/cs/nested_csclasses.aspx.
why he can give me a tutorial?
what should we discuss in Code-project?can you teach me?thank you
shilf 26-May-10 10:35am    
Henry Minute:thanks for your suggestion
Henry Minute 26-May-10 10:36am    
What should we discuss in Code-project? Specific questions, not general discussions. Microsoft, for all their faults, have gone to all the trouble of providing documentation for their products, not to mention all of the already written articles out there on the web. The fact that Abhinav was kind enough to respond to your question does not detract from your unwillingness to do the necessary research.
shilf 26-May-10 20:26pm    
Henry Minute:sorry,I see
shilf wrote:
why f() within class A can be used directly in Main(),but we must create object a to use g()?

This is because f() is static and does not require that an instance of an object to access it.
shilf wrote:
the same;why y within class OuterClass can be used directly in nested NestedClass,but we must create object b to use x?

AFAIK, the inner class is aware of the outer class variables and not vice versa.



It is good that you are trying out such samples and trying to learn the grammar of a language in depth.

However, I would recommend that you get a book and read it or try to search on msdn before posting such questions on a website.
 
Share this answer
 
v2
Comments
shilf 26-May-10 10:31am    
Thanks for your suggestion.
thanks for your "A Tutorial on Nested Classes in C#".
Start with understanding the diference between static and non-static class members.

Don't mix it with nested classes.
 
Share this answer
 

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