Declaring Fields

To declare a field, you must specify at least a type and a field name. Multiple fields with the same type can be put on a single line in a comma-separated manner, just like you can do for variables within a code block. Here, some instance and static fields are declared on a Person class:

class Person{    private static int s_population;    private string _name;    private int _age;}

By default, fields are private, regardless of whether the containing type is a struct or a class. This differs from C++, where classes and structs don’t relate to the allocation location of objects but instead determine the default visibility of members. It’s deemed good style to be explicit about visibility, though. ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.