Saturday, 7 May 2011

Use of Operators, Types and Variables in C#

Operators, types and variables in C#:-
Variables- A variable is a storage location with a type. Variables can have values assigned to them, and those values can be changed programmatically.

   A constant is a variable whose value cannot be changed.
Types-   Like C++ and Java, C# divides types into two sets: intrinsic Built-in types that the language offers and user-defined types that the programmer defines. C# also divides the set of types into two other categories: value types and reference types. The principal difference between value and reference types is the manner in which their values are stored in memory. 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 you put in a variable.
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 is true and false, which are official keywords.

    using System;

    class Booleans
    {
        public static void Main()
        {
            bool content = true;
            bool noContent = false;

            Console.WriteLine("{0} C# programming language content.", content);
            Console.WriteLine("This is second statement {0}.", noContent);
        }
    } 
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.
Floating Point and Decimal Types
A C# floating point type is either a float or double. They are used any time you need to represent a real number. Decimal types should be used when representing financial or money values.
The string Type
A string is a sequence of text characters. You typically create a string with a string literal, enclosed in quotes: "This is an example of a string." You've seen strings being used in Lesson 1, where we used the Console.WriteLine method to send output to the console.
The Array Type
Another data type is the Array, which can be thought of as a container that has a list of storage locations for a specified type. When declaring an Array, specify the type, name, dimensions, and size.

    using System;

    class NewArray
    {
        public static void Main()
        {
            int[] myInts = { 5, 10, 15 };
            bool[][] myBools = new bool[2][];
            myBools[0] = new bool[2];
            myBools[1] = new bool[1];
            double[,] myDoubles = new double[2, 2];
            string[] myStrings = new string[3];

            Console.WriteLine("myInts[0]: {0}, myInts[1]: {1}, myInts[2]: {2}", myInts[0], myInts[1], myInts[2]);

            myBools[0][0] = true;
            myBools[0][1] = false;
            myBools[1][0] = true;
            Console.WriteLine("myBools[0][0]: {0}, myBools[1][0]: {1}", myBools[0][0], myBools[1][0]);

            myDoubles[0, 0] = 4.245;
            myDoubles[0, 1] = 6.355;
            myDoubles[1, 1] = 8.415;
            myDoubles[1, 0] = 56.1148917;
            Console.WriteLine("myDoubles[0, 0]: {0}, myDoubles[1, 0]: {1}", myDoubles[0, 0], myDoubles[1, 0]);

            myStrings[0] = "An";
            myStrings[1] = "App";
            myStrings[2] = "Cattt";
            Console.WriteLine("myStrings[0]: {0}, myStrings[1]: {1}, myStrings[2]: {2}", myStrings[0], 
myStrings[1], myStrings[2]);

        }
    } 

No comments:

Post a Comment