We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, October 15, 2015

Variables and Types in C#

"Variables" are simply storage locations for data. We can place data into them and retrieve their contents as part of a C# expression. The interpretation of the data in a variable is controlled through "Types".

C# is a "Strongly Typed" language. Thus all operations on variables are performed with consideration of what the variable's "Type" is. There are rules that define what operations are legal in order to maintain the integrity of the data we put in a variable.

The C# simple types consist of the Boolean type and three numeric types - Integrals, Floating Point, Decimal, and String. The term "Integrals", which is defined in the C# Programming Language Specification, refers to the classification of types that include sbyte, byte, short, ushort, int, uint, long, ulong, and char. The term "Floating Point" refers to the float and double types, which are discussed, along with the decimal type, in more detail in the Floating Point and Decimal Types section later in this lesson. The string type represents a string of characters and is discussed in The String Type section, later in this lesson. The next section introduces the boolean type.

The Boolean Type


Boolean types are declared using the keyword, bool. They have two values: true or false. In other languages, such as C and C++, boolean conditions can be satisfied where 0 means false and anything else means true. However, in C# the only values that satisfy a boolean condition are true and false, which are official keywords.
Below shows one of the many ways that boolean types can be used in a program.
 Displaying Boolean Values: Boolean.cs

class Booleans
    {
        public static void Main()
        {
            bool content = true;
            bool noContent = false;
            Console.WriteLine("It is {0} that C# Station provides C# programming language content.", content);
            Console.WriteLine("The statement above is not {0}.", noContent);
        }

    }

The boolean values are written to the console as a part of a sentence. The only legal values for the bool type are either true or false, as shown by the assignment of true to the content and false to noContent. When run, this program produces the following output: It is True that C# Program provides C# programming language content. The statement above is not False.

Integral Types

In C#, an integral is a category of types. For anyone confused because the word Integral sounds like a mathematical term, from the perspective of C# programming, these are actually defined as Integral types in the C# programming language specification. They are whole numbers, either signed or unsigned, and the char type. The char type is a Unicode character, as defined by the Unicode Standard. 

Table  shows the integral types, their size, and range.
 The Size and Range of C# Integral Types

TypeSize(in bits)Range
sbyte 8 -128 to 127
byte 8 0 to 255
short 16 -32768 to 32767
ushort 16 0 to 65535
int 32 -2147483648 to 2147483647
uint 32 0 to 4294967295
long 64 -9223372036854775808 to 9223372036854775807
ulong 64 0 to 18446744073709551615
char 16 0 to 65535


Integral types are well suited for those operations involving whole number calculations. The char type is the exception, representing a single Unicode character. As we can see from the table above, we have a wide range of options to choose from, depending on our requirements.
Floating Point and Decimal Types

A C# floating point type is either a float or double. They are used any time we need to represent a real number, Decimal types should be used when representing financial or money values. In table shows the floating point and decimal types, their size, precision, and range.

The Floating Point and Decimal Types with Size, precision, and Range

TypeSize (in bits)precisionRange

float 32 7 digits 1.5 x 10-45 to 3.4 x 1038
double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10308

decimal 128 28-29 decimal places 1.0 x 10-28 to 7.9 x 1028

Floating point types are used when we need to perform operations requiring fractional representations. However, for financial calculations, the decimal type is the best choice because we can avoid rounding errors.

The string Type


A string is a sequence of text characters. We typically create a string with a string literal, enclosed in quotes: "This is an example of a string.
Some characters aren't printable, but we still need to use them in strings. Therefore, C# has a special syntax where characters can be escaped to represent non-printable characters. For example, it is common to use newlines in text, which is represented by the '\n' char. The backslash, '\', represents the escape. When preceded by the escape character, the 'n' is no longer interpreted as an alphabetical character, but now represents a newline.

We may be now wondering how we could represent a backslash character in our code. We have to escape that too by typing two backslashes, as in '\\'.In table shows a list of common escape sequences.
 C# Character Escape Sequences
Escape SequenceMeaning
\' Single Quote
\" Double Quote
\\ Backslash
\0 Null, not the same as the C# null value
\a Bell
\b Backspace
\f form Feed
\n Newline
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab


Another useful feature of C# strings is the verbatim literal, which is a string with a @ symbol prefix, as in @"Some string". Verbatim literals make escape sequences translate as normal characters to enhance readability. To appreciate the value of verbatim literals, consider a path statement such as "c:\\topdir\\subdir\\subdir\\myapp.exe". As we can see, the backslashes are escaped, causing the string to be less readable. We can improve the string with a verbatim literal, like this: @"c:\topdir\subdir\subdir\myapp.exe".

That is fine, but now we have the problem where quoting text is not as easy. In that case, we would specify double double quotes. For example, the string "copy \"c:\\source file name with spaces.txt\" c:\\newfilename.txt" would be written as the verbatim literal @"copy ""c:\source file name with spaces.txt"" c:\newfilename.txt".

No comments: