Advertisement

Literals, Variables and Data Types

By Bintu Chaudhary   Posted at  10:12:00 PM   Unboxing

Home  »  C#  »  Literals, Variables and Data Types


     C# programs are made up of components such as literals, variables, data and the like.

Contents

Literals

     These are the values those are Specified in the Variables or a Literal is a value which is used by variable A value may be either a Integer ,float or a String Variable. A literal may represent any of the following types of values.

Boolean Literals
    There are two boolean literal values: true and false. The type of a boolean-literal is bool.

Integer Literals
    Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows

  1. An Integer Constant must have at Least one Digit
  2. it must not have a Decimal value
  3. it could be either positive or Negative
  4. if no sign is Specified then it should be treated as Positive
  5. No Spaces and Commas are allowed in Name
Real Literals
  1. A Real Constant must have at Least one Digit
  2. it must have a Decimal value
  3. it could be either positive or Negative
  4. if no sign is Specified then it should be treated as Positive
  5. No Spaces and Commas are allowed in Name
  6. Like 251, 234.890 etc are Real Constants

     In The Exponential Form of Representation the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.

  1. In Real Constant The Mantissa and Exponent Part should be Separated by letter e
  2. The Mantissa Part have may have either positive or Negative Sign
  3. Default Sign is Positive
Character Literals

     A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas.
Like 'S' ,'1' etc are Single Character Constants

String Literals

     A String Literal May be a Name or A Sequence of Characters those are written in the double quotes and The String May Contains either digits , characters ,or any Special Symbols.
"Hello C#"

Null Literals

     null-literal
null

The type of a null-literal is the null type.

Variables

     A variable is used for storing a value either a number or a character and a variable also vary its value means it may change his value Variables are used for given names to locations in the Memory of Computer where the different constants are stored. These locations contain Integer , Real or Character Constants.
For example
int a;
float f;
char ch;




Data Types

     Every variable has a data type which denotes the type of data which a variable will hold There are many built in data types in data types those are called as Primitives data types or built in data types and there are Also Some data types those are defined by user defined types which are also called as Non-Primitives data types The data types in C# are of two types:
1) Value Types
2) Reference: Types

Value Types
Values types are also called as Fixed Length data types which stores data on the Stack and a when a Value data type is used only the value of a Variable is Copied by another variable and not the Actual Address will be Passed to a variable Value Types Contains all the Primitives data types and user defined data types Like Numeric , floating and Boolean data types

C# Type
.Net Framework (System) type
Signed?
Bytes Occupied
Possible Values
Sbyte System.Sbyte
Yes
1
-128 to 127
Short System.Int16
Yes
2
-32768 to 32767
Int System.Int32
Yes
4
-2147483648 to 2147483647
Long System.Int64
Yes
8
-9223372036854775808 to 9223372036854775807
Byte System.Byte
No
1
-0 to 255
Ushort System.Uint16
No
2
-0 to 65535
Uint System.UInt32
No
4
-0 to 4294967295
Ulong System.Uint64
No
8
-0 to 18446744073709551615
Float System.Single
Yes
4
Approximately +-1.5 x 10-45 to +-3.4 x 1038 with 7 significant figures
Double System.Double
Yes
8
Approximately +-5.0 x 10-324 to +-1.7 x 10308 with 15 to 16 significant figures
Decimal System.Decimal
Yes
12
Approximately +-5.0 x 10-28 to +-7.9 x 1028 with 28 to 29 significant figures
Char System.Char
N/A
2
Any Unicode character (16 bit)
Bool System.Boolean
N/A
1 / 2
true or false



Reference Types

     Reference data types are Always Stored on the heap and when we uses heap then the Actual Location is Copied or Same Memory Location for a Single Value Will be used and you can also say that Reference variables are used when we wants to affect on the original values of the variable




Boxing and Unboxing

     This is the newly Concept which is Provides by C# Boxing is used when we wants to Convert the Variable of any data type into the Object data type or When we wants to Make a Simple Variable as an object Variable and Unboxing is the Process when we wants to Converts back an Object Variable into a Specific Variable For Boxing as we convert a integer into a Float , we can Convert a variable into an object data type but when we wants to Convert Object into Specific data type then we have to use Explicit Conversion or we have to Convert the Value by Putting Name of data type in which we wants to Convert



Boxing Coversions

     A boxing conversion permits any value-type to be implicitly converted to the type object or to any interface-type implemented by the value-type. The boxing class would be declared as follows:
//Boxing an Integer Variable




Output
The Value-Type value=456
The Object-Type Value=123




Unboxing Coversions

     An unboxing conversion permits an explicit conversion from type object to any value-type from or from any interface-type to any value-type that implements the interface-type
//Unboxing Conversion





About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

Back to top ↑
Connect with Me