C# Fields

  • C# fields are class or struct level variables which can be declared in any C# types including built-in types or user defined types.
  • C# fields includes instance fields and static fields. They can be defined together in one class or struct.
  • Instance fields can hold different values of their defined types for different instances without affecting each other.
  • Static fields are declared in the class level and can be shared in the code which can access them.
  • C# fields are recommended to have private or protected modifier and can be accessed by properties or methods.
  • C# fields are defined to be accessed by more than one methods.If they are only used in one method, you'd best to define local variables instead of class fields.

Syntax of Declaring a field:

<Access Modifier> <Data Type> <Field Name> [=<value>];

Syntax of Access an object field:

<object name>.<field name>

When creating an object of a class, the fields of the objects will defined as their default values if they are not initialized when declaring. For the default value of built-in types, you can check C# data types. For class types, the default value of the field is null.

Example 01-42-01

In this example, we'll show you the default values of the fields in some data types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
class TestDefault
{
    public bool b;
    public int a;
    public long l;
    public float f;
    public double d;
    public char c;
    public string s;
    public object o;

    static void Main(string[] args)
    {
        TestDefault td = new TestDefault();
        Console.WriteLine("b is {0}", td.b);
        Console.WriteLine("a is {0}", td.a);
        Console.WriteLine("l is {0}", td.l);
        Console.WriteLine("f is {0}", td.f);
        Console.WriteLine("d is {0}", td.d);
        Console.WriteLine("c is {0}", td.c == '\0' ? "\\0" : "Unknown");
        Console.WriteLine("s is {0}", td.s == null ? "null" : "Unknown");
        Console.WriteLine("o is {0}", td.o == null ? "null" : "Unknown");
		
        Console.Read();
    }
}

Output

b is False
a is 0
l is 0
f is 0
d is 0
c is \0
s is null
o is null

Explanation

  • Line 4-11: Declare several different types of fields without initialized.
  • Line 13: Main method starts.
  • Line 15: Create an instance of the class "TestDefault".
  • Line 16-23: Output the default values of the fields.
  • Line 21-23: Because null or \0 can not be seen in the output, we'll use ?: operator to output the string if the fields equal to their default values.
  • Check the result, each field outputs its default value as expected.

Actually, when compiling the above example, you will get the following warnings. This is another way to know the default values of the fields.

test.cs(6,17): warning CS0649: Field 'TestDefault.b' is never assigned to, and will always have its default value false
test.cs(7,16): warning CS0649: Field 'TestDefault.a' is never assigned to, and will always have its default value 0
test.cs(8,17): warning CS0649: Field 'TestDefault.l' is never assigned to, and will always have its default value 0
test.cs(9,18): warning CS0649: Field 'TestDefault.f' is never assigned to, and will always have its default value 0
test.cs(10,19): warning CS0649: Field 'TestDefault.d' is never assigned to, and will always have its default value 0
test.cs(11,17): warning CS0649: Field 'TestDefault.c' is never assigned to, and will always have its default value 
test.cs(12,19): warning CS0649: Field 'TestDefault.s' is never assigned to, and will always have its default value null
test.cs(13,19): warning CS0649: Field 'TestDefault.o' is never assigned to, and will always have its default value null

Example 01-42-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

namespace TestField
{
    class House
    {
        public int area;
        public string unit = "SQFT";
        public bool isDetached;
    }
	
    class TestHouse
    {
        static void Main(string[] args)
        {
            House h1 = new  House();
            House h2 = new  House();

            h1.area = 1500;
            h1.isDetached = false;
            h2.area = 2200;
            h2.isDetached = true;

            Console.WriteLine("House 1 is {0} with {1} {2}.", h1.isDetached ? "Detached" : "Attached", h1.area, h1.unit);
            Console.WriteLine("House 2 is {0} with {1} {2}.", h2.isDetached ? "Detached" : "Attached", h2.area, h2.unit);
            Console.Read();
        }
    }
}

Output

House 1 is Attached with 1500 SQFT.
House 2 is Detached with 2200 SQFT.
  • Line 5-10: Define a class House.
  • Line 7: The field area is declared as int with default value 0.
  • Line 8: The field unit is declared as string and initialized value with "SQFT".
  • Line 9: The field isDetached is declared as bool with default value false.
  • Line 14: The program starts with Main method.
  • Line 16-17: Created 2 instances of the class House.
  • Line 19-22: Assign values to these fields to change the default values.
  • Line 24-25: Output the result. We're using ? operator to judge if the house is detached or attached.
  • Check the result you will see the fields of the instances output what they defined.