C# field initialization

Description

Field initialization is optional.

An uninitialized field has a default value (0, \0, null, false). Field initializers run before constructors:

public int Age = 10;

Multiple fields

For convenience, you may declare multiple fields of the same type in a comma-separated list.

This is a convenient way for all the fields to share the same attributes and field modifiers.

For example:


static readonly int field1 = 8, 
                    field2 = 1; 




















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor