Game Development Reference
In-Depth Information
Listing 10-15. Automatic Properties
class MyData
{
//Public property
public string Name{get; set;}
}
We could take this even further and create read-only and write-only automatic properties. Although
the access levels apply only to other classes accessing the properties, and not to the owner class
itself. Listing 10-16 creates a read-only property, and Listing 10-17 creates a write-only property
(if you'd really need such a thing!).
Listing 10-16. Read-Only Automatic Properties
class MyData
{
//Public property
public string Name{get; private set;}
}
Listing 10-17. Write-Only Automatic Properties
class MyData
{
//Public property
public string Name{private get; set;}
}
C# Features or Quirks?
Let's now consider some other, more controversial, features of C# that are helpful for coding games,
such as when expanding on CMOD, or are at least useful to know, if nothing else. These features are
controversial in the sense that some developers cannot see or don't accept their usefulness in any
circumstances, except for the most obscure or remote cases, while others declare their usefulness in
many circumstances. Let's see what these are.
Private Does Not Mean Inaccessible
Let's say you declare a class with a private variable, as shown in Listing 10-18. You may think that
the variable is protected in that no other instances could ever access it, but for the instance to which
it belongs. The variable is not public but private, and so surely no other instances can access it,
right? Wrong.
Listing 10-18. A Class with a Private Variable
public class MyClass
{
private string Name = "DefaultString";
}
 
Search WWH ::




Custom Search