Coming from C++ programming and having read that protection rules are almost the same, since there is a single class I would expect it to work even if both the That struct and the Demo method were private.

As the side note, a link to some page summing up scope and protection rules toward the C++ programmer would be greatly appreciated.

See Cody's answer; there is a big difference between structs and classes in C#; they are barely related, in fact. Most times that people use a struct in C#, it is incorrect. Make sure you are using it for the right reason. "Because it is a simple object" is not such a reason.
–
Marc Gravell♦Mar 28 '11 at 8:55

@BoltClock: ;) @Marc: will "because I just want a quick array of scalar pairs" do as an excuse?
–
Julien GuertaultMar 28 '11 at 9:02

Yes, I'd say so. Not necessarily because it's simple or quick, but because you want value type semantics for those scalar pairs. They should work just like a regular int, not like an object. It makes logical sense that they be immutable. I'd choose a struct for that, too.
–
Cody GrayMar 28 '11 at 9:05

1

On a tangential note: In the case that you don't want your internals public but need them to be public anyhow, InternalsVisibleTo allows you to make an internal class visible to a specific external assembly. This is mostly useful for unit-testing.
–
BrianMar 28 '11 at 15:19

5 Answers
5

The other answers given already have your answer, so I won't beat a dead horse here. You need to declare the field public in order to be able to access it from external code.

In C++, structs and classes are equivalent, with the only difference being the default access level of their respective members.

However, that's not the case in C#. Generally, you should only use a struct for small, short-lived objects that are immutable (won't change). A structure has value type semantics, where as a class has reference type semantics. It's very important that you understand the difference between value types and reference types if you're learning to program in C#. Jon Skeet has published an article that attempts to provide that explanation. But you'll definitely want to pick up a good introductory book to C# that treats these issues in more detail.

More often than not, you'll want to use a class in C#, rather than a structure. And when you use that class, note that Microsoft's design guidelines for C# tend to recommend against exposing public fields. Instead, they recommend that you use a public property, backed by a private field. A more thorough exposition of the rationale behind that guideline is given here. For example:

Yep, for the sake of the example I did put public, but in real life things go as private as possible indeed, and this ability to define set/get clearly helps. Thank you for the overview and link.
–
Julien GuertaultMar 28 '11 at 8:55

1

@Julien: Of course. I assumed the example was contrived for demonstration purposes only. Obviously in the real world there are indeed legitimate cases where you want to use a struct. Not only for value type semantics, but also for interop purposes. But in general, objects are best modeled using a class in C#.
–
Cody GrayMar 28 '11 at 8:57

@Bolt: But of course, I assume that you've placed all those structs in a separate namespace and/or assembly, and declared them either private or internal. I always write a nice, clean managed wrapper class over the interop code, which is the only thing I expose publically.
–
Cody GrayMar 28 '11 at 9:03