aronmatthew
Well-known member
- Joined
- Aug 5, 2019
- Messages
- 67
- Programming Experience
- Beginner
// #define DO_NOT_INIT
using System;
using System.Drawing;
abstract class Vehicle
{
#if DO_NOT_INIT
protected static Color s_color;
#else
protected static Color s_color = Color.Green;
#endif
}
class Boat : Vehicle
{
public Boat()
{
Console.WriteLine(s_color);
}
}
public class Program
{
public static void Main()
{
var boat = new Boat();
}
}
declaring and initializing a static reference type
Color
is not a reference type. It is a value type.F117
class here.using System;
using System.Drawing;
abstract class Vehicle
{
protected static Color s_color = Color.Green;
}
class Boat : Vehicle
{
public Boat()
{
Console.WriteLine(s_color);
}
}
class F117 : Vehicle
{
public F117()
{
s_color = Color.Black;
Console.WriteLine(s_color);
}
}
public class Program
{
public static void Main()
{
var boat = new Boat();
var f117 = new F117();
var boat2 = new Boat();
}
}
Color [Green]
Color [Black]
Color [Black]
readonly
. Make line 6:protected static readonly Color s_color = Color.Green;
Compilation error (line 21, col 3): A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)