Complete the code to declare a constant integer named MaxValue with value 100.
public class Config { public const int MaxValue = [1]; }
The keyword const declares a constant value that cannot change. Here, MaxValue is set to 100.
Complete the code to declare a readonly field named creationTime initialized in the constructor.
public class Logger { public readonly DateTime creationTime; public Logger() { creationTime = [1]; } }
The readonly field creationTime can be assigned in the constructor. DateTime.Now gives the current time.
Complete the code to initialize the readonly field named configName in the constructor.
public class Settings { public readonly string configName; public Settings(string name) { configName = [1]; } }
The readonly field configName can only be assigned at declaration or inside constructors. Assign the constructor parameter name to it.
Fill both blanks to declare a constant double Pi and a readonly double radius initialized in the constructor.
public class Circle { public const double Pi = [1]; public readonly double radius; public Circle(double r) { radius = [2]; } }
Pi is a constant set to 3.14159. The readonly field radius is assigned the constructor parameter r.
Fill all three blanks to declare a constant int MaxScore, a readonly int currentScore initialized in the constructor, and a method returning their sum.
public class Game { public const int MaxScore = [1]; public readonly int currentScore; public Game(int score) { currentScore = [2]; } public int TotalScore() { return MaxScore + [3]; } }
MaxScore is a constant set to 100. The readonly field currentScore is assigned the constructor parameter score. The method TotalScore returns the sum of MaxScore and currentScore.