C# Constants

In CSharp, 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. CSharp 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.

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"; 		
	}
}


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.