Serving Information Simply

Monday, 21 January 2013

Literals in C#.Net


Literals or constants are the values we write in a conventional form whose value is obvious. In contrast to variables, literals (123, 4.3, “hi”) do not change in value. These are also called explicit constants or manifest constants. I have also seen these called pure constants, but I am not sure if that terminology is agreed on. At first glance one may think that all programming languages type their literals the same way.
Literals represent the possible choices in primitive types for that language. Some of the choices of types of literals are often integers, floating point, Boolean and character strings. Each of these will be discussed in this Topic.

Definition - The values that we use from our side for assignment or some expression is called as literal.

Types with Examples…

o   Integral Literals
§  Default is int
·         int num=67;
§  Use l or L with long and u or U with unsigned as suffix
·         long k=67L;
·         uint num=78U;
o   Floating
§  Default is double
·         double x=5.6;
§  Use f or F with floats and m or M with decimal as suffix
·         float y=5.7; //compile time code
·         float y=5.7f;
·         decimal k=6.7M;

o   Character Literals
§  Enclosed in Single Quotes
·         char ch=’A’;
·         char ch=65; // compiler time error
·         char ch=(char)65; //correct

o   String literals
§  Enclosed in double quotes
·         string name=”Abhinav”;
§  Strings are managed by String class
§  We can also use null to show no reference

o   Boolean literals
§  Can be true or false only
§  Default is false
·         bool married=true;

Note: true, false and null are the literal value and not the keywords

Thanks keep visiting.......

No comments:

Post a Comment