Constant : Example

CSharp · Console

/Program.cs

using System;

public class Program
{
	const int b = 5; // compile-time constant field
	
	//Not possible - must assign a value at time of declaration
	//private const string name;
	
	public static void Main()
	{
		// Not possible - must assign at time of declaration.
		//name = "Pirzada";
		
		Console.WriteLine($"Constant field b = {b}");
		
		const int a = 10; // compile-time local constant
		Console.WriteLine($"My local constant a = {a}");
		
		//using a class name prefix 
		Console.WriteLine($"MyClass.name = {MyClass.name}");
	}
}

class MyClass{
	
	public const string name = "Pirzada";
	
}
;