Hi everybody! I'm currently studying for exam 70-316, following the second edition of the official training kit. Page 28 of said book says "If the Dim statement is used (in Visual Basic .NET) or no access modifier is used (in Visual C#), the variable is considered private in Visual C# and Visual Basic .NET classes, Public in Visual Basic .NET structures, and private in Visual C# structures. Methods do not require an access modifier. If no access modifier is specified, the method is Private (private) by default in a class or structure in C#, and Public (public) in a class or structure in Visual Basic .NET." And from this, the following table can be drawn: | Visual Basic .NET | Visual C# ----------+-----------------+-------------------+----------- | member variable | private | private class +-----------------+-------------------+----------- | method | public | private ----------+-----------------+-------------------+----------- | member variable | public | private structure +-----------------+-------------------+----------- | method | public | private However, page 32 says "If an access modifier is not specified for a method, it has a default access level of private in Visual C# classes and structures and public in Visual Basic .NET classes and structures. If an access modifier is not specified for a member variable, it has a default access level of private in a class or public in a structure." And from this the following table can be drawn: | Visual Basic .NET | Visual C# ----------+-----------------+-------------------+----------- | member variable | private | private class +-----------------+-------------------+----------- | method | public | private ----------+-----------------+-------------------+----------- | member variable | public | public structure +-----------------+-------------------+----------- | method | public | private So there must be something wrong somewhere, because a member variable of a structure in Visual C# is public or private, depending on where the book is read. Can someone clarify this? Thanks in advance!
Looks like a struct field is private by default. You can use the System.Reflection.FieldInfo class to check this out for yourself. Create a console app, replace the default code in Class1.cs with the code below and press F5. (Code was based on sample code in the MSDN FieldInfo topic) using System; using System.Reflection; class Tester { public static void Main() { MyStruct x = new MyStruct(); x.MyMethod(); Console.WriteLine("Press <Return> to exit..."); Console.ReadLine(); } } public struct MyStruct { int myField1; public void MyMethod() { FieldInfo[] myFieldInfo; Type myType = typeof(MyStruct); // Get the type and fields of MyStruct. myFieldInfo = myType.GetFields (BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); Console.WriteLine("\nThe fields of MyStruct are \n"); // Display the field information of MyStruct. for(int i = 0; i < myFieldInfo.Length; i++) { Console.WriteLine("\nName : {0}", myFieldInfo.Name); Console.WriteLine("Declaring Type : {0}", myFieldInfo.DeclaringType); Console.WriteLine("IsPublic : {0}", myFieldInfo.IsPublic); Console.WriteLine("MemberType : {0}", myFieldInfo.MemberType); Console.WriteLine("FieldType : {0}", myFieldInfo.FieldType); Console.WriteLine("IsFamily : {0}", myFieldInfo.IsFamily); } } }