473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

?? '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

Oct 31 '07 #1
13 2109
Tom Baxter wrote:
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.
I think if you change your definition to actually hide a member (M) you
will remove that error.
class MyClass
{
public virtual int M() { return 1; }
}

class Outer : MyClass
{
public new class M : MyClass
{
}
}

class C2 : MyClass
{
public new struct M
{
}
}
Oct 31 '07 #2
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc

Oct 31 '07 #3
Thanks, Marc!! I appeciate!
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #4
I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #5
Tom Baxter <tl********@newsgroup.nospamwrote:
I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??
Structs cannot contain protected members at all - because you can't
derive from a struct. The fact that the member itself is a struct is
just coincidental - you'll see the same behaviour if B is a variable or
a method.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 31 '07 #6
You can't inherit from a struct - in particular, you can't inherit
from struct A; hence "protected" is meaningless. You would get similar
(actually just a warning, not an error) if you marked class A as
"sealed": new protected member declared in sealed class.

Marc

Oct 31 '07 #7
It turns outy that a struct *can* be marked as protected but only of its
immediate parent is a class. If its immediate parent is a struct, it cannot
be protected. So, this does not compile:

class A {
class B {
struct C {
protected struct Z { }
}
}
}

while this does:

struct A {
struct B {
class C {
protected struct Z { }
}
}
}

So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc
--
Tom Baxter

Oct 31 '07 #8
On 2007-10-31 16:35:19 -0700, "Tom Baxter" <tl********@newsgroup.nospamsaid:
So, why would it be the case that a nested struct can be protected if
it's within a class but not if it's within a struct??
You are not focusing on the important part. Any class can be
inherited. So any declaration within a class may have the "protected"
access.

It doesn't matter where the class is. You can declare it in a struct
if you like, the rule still applies.

Class C can't be "protected", because it's a member of a struct.
Struct B can't be "protected" because it's a member of a struct. But
Struct Z is a member of a class. Thus, it can be "protected".

It's the same exact rule that applies to the general case that started
this thread. Nothing's different about this most recent example.

Pete

Nov 1 '07 #9
How stupid of me! It's rather embarrassing, actually.

Thank you Jon, Marc and Pete. I sincerely appreciate you taking the time to
respond.

Jon, I am looking forward to your Manning book. Do you have a ballpark time
frame as to when it will be available in hard copy?

"Tom Baxter" <tl********@newsgroup.nospamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>I guess I spoke too soon! :)

This will compile:

class A {
protected struct B {}
}
but this will not:

struct A {
protected struct B {}
}
So, why would it be the case that a nested struct can be protected if it's
within a class but not if it's within a struct??

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@50g2000hsm.googlegro ups.com...
>And on the "protected" side - it again relates to things that subclass
the containing class - i.e.

public class BaseClass {
void TestBase() {
NestedStruct ns;
}
private struct NestedStruct { int a;}
}
public class SubClass : BaseClass {
void TestSub() {
NestedStruct ns; // **ERROR
}
}

The above is illegal, since SubClass cannot see BaseClass.NestedStruct
(since it is private); however, change NestedStruct to be protected,
and any sublasses of BaseClass can now see it.

Marc

--
Tom Baxter
--
Tom Baxter

Nov 1 '07 #10
How stupid of me! It's rather embarrassing, actually.

Not at all; you weren't entirely sure of something, so you a: read the
spec, b: did some tests of your own to see how it behaved, and c:
asked relevant questions (with examples for illustartion) until it
"clicked". Sounds like a pretty sensible approach to me! Any other
questions, please ask away...

Marc

Nov 1 '07 #11
On 2007-11-01 00:14:29 -0700, Marc Gravell <ma**********@gmail.comsaid:
>How stupid of me! It's rather embarrassing, actually.

Not at all; you weren't entirely sure of something, so you a: read the
spec, b: did some tests of your own to see how it behaved, and c:
asked relevant questions (with examples for illustartion) until it
"clicked". Sounds like a pretty sensible approach to me! Any other
questions, please ask away...
Agreed. If only all questions posted here were approached by the
questioner so methodically and open-mindedly.

I'm always a little embarassed when I find out I read something wrong.
I suppose that's human nature. But in reality, as they say...the only
dumb question is the one you didn't ask. :) I never mind answering a
question posed by someone who is clearly sincere about the question,
and who is doing their best to present their question in an effective
way, as was the case here.

Pete

Nov 1 '07 #12
Tom Baxter <tl********@newsgroup.nospamwrote:
How stupid of me! It's rather embarrassing, actually.

Thank you Jon, Marc and Pete. I sincerely appreciate you taking the time to
respond.

Jon, I am looking forward to your Manning book. Do you have a ballpark time
frame as to when it will be available in hard copy?
March 2008 - Amazon claims the middle of March, but I suspect it'll be
the end of March instead. No doubt it will partly depend on how
speedily the last phases (indexing, proof reading etc) go.

Nice to know you're looking forward to it though :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 1 '07 #13
Marc Gravell <ma**********@gmail.comwrote:
How stupid of me! It's rather embarrassing, actually.

Not at all; you weren't entirely sure of something, so you a: read the
spec, b: did some tests of your own to see how it behaved, and c:
asked relevant questions (with examples for illustartion) until it
"clicked". Sounds like a pretty sensible approach to me! Any other
questions, please ask away...
Absolutely. Frankly, anyone who asks a question referring to the spec
is streets ahead of the game in my view :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 1 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Screwball | last post by:
<?php class myClass { var $myVar; .. .. } ?> In php.ini I have error_reporting = E_ALL | E_STRICT
2
by: Kolozs, Áron | last post by:
Hi everybody, The C# compiler reports a Compiler Error CS0052 in the following situation: I declared a type marked as "internal": namespace MyNamespace { internal class MyInteralClass
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
3
by: Shawn B. | last post by:
Greetings, I'm trying to create a Managed C++ library that extends the console features beyond System::Console capabilities. I chose MC++ over C++/CLI because I need it to work on the 1.1...
7
by: My4thPersonality | last post by:
1st, simple question, is there a protected in C#? I mean a comparable keyword like the C++ protected, which defines a member as accessable for decendants, but not for the outside world. Then,...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
4
by: newbie120 | last post by:
Hi all maybe its just been a long day, but i have a question about call access modifiers in C#. Consider the following code. namespace Application { private class Class1 { int i;
13
by: Clive Dixon | last post by:
I am refactoring some code to move some base classes into a separate assembly. One of these base classes has a member property which is 'protected internal'. However when I move these base classes...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.