C# Examples

C# Example Code


C# Constants

In C#, const keyword is used to declare a constant field or a constant local it is similar to variable except const keywords are used to variable declaration. Constant and locals aren't variables and cannot be modified. Constants can be any numbers, boolean, strings, or a null reference. C# constant are used to store the modal number of a device, a product version number, or the brand name of a company. Constants are particularly useful if there is a value which is used repeatedly throughout the program code. Remember that static modifier is not allowed in a constant declaration.

The differenc between readonly and const, a const field can only be initialized at the declaration time of the field, whereas a readonly field can be initialized either at the declaration time as well as in a constructor.

C# const Keyword Example

using System;
class DataTypeDemo
{
	static void main()
	{
		public const int var1 = 52;
		public const int var2 = var1 + 211; 
		public const url = "www.techstrikers.com"; 		
	}
}