T
Tom Baxter
Hi everyone,
Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and
'protected' are valid modifiers on struct declarations. First, how can
'protected' be valid on a struct, since structs cannot be inherited? The
compiler gives an error (as I expect it should) if you try this:
protected struct MyStruct {}
so I'm wondering if the spec is wrong when it says 'protected' is a valid
struct-modifier.
The second point about section 18.1.1 is that it indicates 'new' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The modifiers
of a struct declaration have the same meaning as those of a class
declaration", referring the reader to section 17.1.1 on class declaration
modifiers.
I looked at section 17.1.1 and sure enough, it says:
"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."
My testing indicates 'new' is not allowed on class nor struct declarations
(nested or not). It seems this is how 'new' should be used as a class
modifier, according to the spec (sec. 17.1.1):
class MyClass {
public virtual int M() { return 1; }
}
class Outer {
new class Inner : MyClass { // ERROR on 'new' modifier
public new int M() { return 2; }
}
}
The above code gives a warning on the declaration of Inner.
So, am I way off base here? Is there something wrong with sections 18.1.1
and 17.1.1?
Thanks
Has anyone looked at section 18.1.1 of the C# spec? It indicates 'new' and
'protected' are valid modifiers on struct declarations. First, how can
'protected' be valid on a struct, since structs cannot be inherited? The
compiler gives an error (as I expect it should) if you try this:
protected struct MyStruct {}
so I'm wondering if the spec is wrong when it says 'protected' is a valid
struct-modifier.
The second point about section 18.1.1 is that it indicates 'new' is an
allowable modifier on a struct. Actually, 18.1.1 indicates, "The modifiers
of a struct declaration have the same meaning as those of a class
declaration", referring the reader to section 17.1.1 on class declaration
modifiers.
I looked at section 17.1.1 and sure enough, it says:
"The new modifier is permitted on nested classes.
It specifies that the class hides an inherited member
by the same name, as described in section 17.2.2. It
is a compile-time error for the new modifier to appear
on a class declaration that is not a nested class declaration."
My testing indicates 'new' is not allowed on class nor struct declarations
(nested or not). It seems this is how 'new' should be used as a class
modifier, according to the spec (sec. 17.1.1):
class MyClass {
public virtual int M() { return 1; }
}
class Outer {
new class Inner : MyClass { // ERROR on 'new' modifier
public new int M() { return 2; }
}
}
The above code gives a warning on the declaration of Inner.
So, am I way off base here? Is there something wrong with sections 18.1.1
and 17.1.1?
Thanks