static classes

P

puzzlecracker

It looks like static classes can have non-static member classes --
does it make sense what I just wrote? If not, let me illustrate it:

public static class Foo{

private struct Bar{

}

}

I wonder why this is allowed.

Also, what is the difference between static and non-static nested
classes?

public class Foo{

private class Bar
{

}
}

vs.
public class Foo{

static private class Bar
{

}
}

thanks
 
J

Jon Skeet [C# MVP]

It looks like static classes can have non-static member classes
Yes.

does it make sense what I just wrote? If not, let me illustrate it:

public static class Foo{

   private struct  Bar{

  }

}

I wonder why this is allowed.

Why wouldn't it be? In some ways it's still "static" in that a nested
type isn't associated with any instance of its containing type. Note
that this is different from inner classes in Java.
Also, what is the difference between static and non-static nested
classes?

public class Foo{

   private class Bar
   {

  }

}

vs.
public class Foo{

  static  private class Bar
   {

  }

}

The latter is just like any other static class: it can only have
static members, it doesn't have any constructors (not even an
autogenerated one), you can't declare a variable of that type, etc.

Jon
 
P

puzzlecracker

Why wouldn't it be? In some ways it's still "static" in that a nested
type isn't associated with any instance of its containing type. Note
that this is different from inner classes in Java.










The latter is just like any other static class: it can only have
static members, it doesn't have any constructors (not even an
autogenerated one), you can't declare a variable of that type, etc.

Jon

Hmm, perhaps I missing the point of static nested classes vs. non-
static nested classes
 
I

Ignacio Machin ( .NET/ C# MVP )

It looks like static classes can have non-static member classes --

Of course. Why would you be forced to declare it static?
think of this:
static class Factory{
public class Internal{}
public static Factory.Internal GetIt(){ return new Internal();}
}
does it make sense what I just wrote? If not, let me illustrate it:

public static class Foo{

private struct Bar{

IIRC a struct cannot be marked as static so there goes one more issue
of why not restrict it to statics
 
P

puzzlecracker

Of course. Why would you be forced to declare it static?
think of this:
static class Factory{
  public class Internal{}
  public static Factory.Internal  GetIt(){ return new Internal();}

}



IIRC a struct cannot be marked as static so there goes one more issue
of why not restrict it to statics

What is difference between inner static class and member nested class?
 
J

Jon Skeet [C# MVP]

What is difference between inner static class and member nested class?

A static class (whether nested or not) has additional restrictions on
it - basically, it's there as a utility class, and will never be
instantiated. The "static" part of the declaration of a nested static
class is very different from the static part of the declaration of a
static variable, property etc.

Jon
 
P

puzzlecracker

A static class (whether nested or not) has additional restrictions on
it - basically, it's there as a utility class, and will never be
instantiated. The "static" part of the declaration of a nested static
class is very different from the static part of the declaration of a
static variable, property etc.

Jon

Well, in java nested static and nested member class are different in
regard what they can access.... i guess csharp doesn't have
static static class...

class A
{

static static class B
{

}

}
 
P

puzzlecracker

No, that's not true.  The only difference between static nested and inner  
classes in Java is that an inner class has an implicit reference to an  
instance of the outer class.  Both kinds of classes have the same access  
to members of the outer class, though of course without an implicit  
reference, the static nested class would need to be provided an explicit  
reference to work with.

For example (in Java):

public class Outer
{
     private int i;

     private class Foo
     {
         public int getOuterI()
         {
             return Outer.this.i;
         }
     }

     static private class Bar
     {
         public int getOuterI(Outer outer)
         {
             return outer.i;
         }
     }

}

C# uses the word "static" only to mean that a class cannot be  
instantiated.  It has no concept of an inner class (all nested classes are  
equivalent to Java's static nested class), so the "static" keyword isn't  
used in the same way.


Why would it?  What would that mean?

Pete

Pete, thanks that explains....
 
P

Petar Repac

puzzlecracker said:
It looks like static classes can have non-static member classes --
does it make sense what I just wrote? If not, let me illustrate it:

public static class Foo{

private struct Bar{

}

}

I wonder why this is allowed.
Because Foo is static you can't create instances of this type, but Foo
can make instances of Bar, for example is it's static fields.

Also, what is the difference between static and non-static nested
classes?

public class Foo{

private class Bar
{

}
}

vs.
public class Foo{

static private class Bar
{

}
}

thanks

The later just means that you can not make instances of Bar, so only Foo
can use it (it's private) and only call it's static members.

Petar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top