C# field initialization

In this chapter you will learn:

  1. What is field initialization
  2. Declaring multiple fields together

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; 

Next chapter...

What you will learn in the next chapter:

  1. What is a constant
  2. How to create a constant field
  3. Example for defining a constant field
  4. Local Constants
Home »
  C# Tutorial »
    C# Types »
      C# Class Field
C# class field
C# Field default value
C# field initialization
C# Constant
C# Read only field
C# Static Fields