?? 'new' and 'protected' Modifiers on Structs ??



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
--
Tom Baxter

.



Relevant Pages

  • Re: ?? new and protected Modifiers on Structs ??
    ... It indicates 'new' and 'protected' are valid modifiers on struct declarations. ... 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. ... public virtual int M ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to pass a struct to a function
    ... write an expression evaluating to your struct -- the name of a variable ... you haven't declared a type "struct entry". ... about how this declaration is handled, ... on which compiler you're using, but any decent compiler will print ...
    (comp.lang.c)
  • Re: Passing Structure to a function
    ... > int Menu; ... 'struct' keyword above is because otherwise you got compiler errors. ... The reason for the errors is that phonerec hasn't been declared or ... A structure declaration merely says that a structure ...
    (comp.lang.cpp)
  • Re: syntax errror
    ... >>> int maxGrey' ... >> declaration of maxGrey. ... typedef double; ... struct some_tag_name { ...
    (comp.lang.c)
  • Re: mutually referential (Pg 140 K&R2)
    ... >In this example the vacuous declaration doesn't make any difference; ... >needed when you want a name in an inner scope to match a tag defined ... >in the inner scope instead of a tag from the outer scope. ... the vacuous declarations of struct ...
    (comp.lang.c)