Datatypes

C# datatypes falls into following two categories:
  1. Value Type
  2. Reference Type
Value Type:
It Contains all built-in datatypes.The content of a value type variable or constant is simply a value.The data is stored in the stack memory.
Value Type datatypes are Numerical, Floating point, Character, Logical datatypes and as well as custom struct and enum types.


Reference Type:
Reference types contains all class, array, delegate, and interface types.The data is stored in heap memory. Reference type variable contains the address of the data..

Datatype specifies the size and type of data. Every programming language will have their own datatypes.
According to the size and type of a data C#.NET datatypes are classified into five types. 
  1. Numerical datatypes 
  2. Floating point datatypes
  3. Character datatypes
  4. Logical  datatypes
  5. General datatypes (Reference Type)
       First four are value type.

Numerical Datatypes:
The number which is not having the fractional part is called as numerical datatypes.
Numerical datatypes are classified into 2 types
  1. Signed Datatypes
  2. Unsigned Datatypes

Example:
        int x = 127; // decimal notation
       long y = 0x7F; //hexdecimal notation

Floating point datatypes:
The numbers which are having the fractional part that comes under floating point datatypes.
Floating point datatypes are
  1. Float
  2. Double
  3. Decimal

The F and M suffixes are the most useful and should always be applied when specifying float or decimal literals. Without the F suffix, the following line would not compile, because 4.5 would be inferred to be of type double, which has no implicit conversion to float:
            float f = 4.5F;
The same principle is true for a decimal literal:
           decimal d = −1.23M; // Will not compile without the M suffix.

Logical datatypes:
Bool: It holds either True or  False. True representing with '1' and False representing with '0'. The predefined size for bool is 1 bit. Base type for bool is System.Boolean.

Equality and Comparison Operators:
                == and != test for equality and inequality of any type, but always return a bool value.Value types typically have a very simple notion of equality
Example:
             int x = 1;
             int y = 2;
             int z = 1;
             Console.WriteLine (x == y); // False
             Console.WriteLine (x == z); // True
For reference types, equality, by default, is based on reference, as opposed to the actual value of the underlying object.
                  public class Program
                  {
                      public string Name;
                      public program(string n)
                     { 
                        Name = n;
                      }
                 }
                 public class ProgramMain
                {
                     public static void Main()
                    {
                       Program p1 = new Program("sai");
                       Program p2 = new Program("sai");
                       Console.WriteLine(p1 == p2); // False
                       Program p3 = p1;
                      Console.WriteLine(p1 == p3); // True
                    }
               }

The equality and comparison operators, ==, !=, <, >, >=, and <=, work for all numeric types.

Conditional Operators:
The && and || operators test for and and or conditions.
Example:
     int a=10;
     if(a!=null && a>11)
     Console.writeLine("Highere value");  //flase
     else
     Console.writeLine("Lower value");  //true

Ternary conditional: 
The ternary conditional operator has the form q ? a : b, where if condition q is true, a is evaluated, else b is evaluated. 
Example:
             static int number (int a, int b)
             {
                 return (a > b) ? a : b;
             }

Character datatype:
Char:
Character datatype represents the unicode character.A char literal is specified inside single quotes. Predefined size for char is 2 bytes, it holdes 16 bits of unicode characters. Base type is System.Char.
       Example:  char c = 'V';

Escape sequences express characters that cannot be expressed or interpreted literally. An escape sequence is a backslash followed by a character with a special meaning.
Example:
char a = '\n'; // Moves to new line 
char c= '\\';  // Back Slash
Below table shows some of the escape sequences

General Datatypes:
These are of two types
1) String: It represents collections of unicode characters.A string literal is specified inside double quotes.
Example: string name ="sairam";

        String concatenation:
                  The + operator concatenates two strings:
                  string s = "sai" + "ram"

The escape sequences that are valid for char literals also work inside strings
               string a = "gets on tab space:\t";

The cost of this is that whenever you need a literal backslash, you must write it twice:
          string c = "\\\\programs\\csharp\\firstprogram.cs";

To avoid this problem, C# allows verbatim string literals. A verbatim string literal is prefixed with @ and does not support escape sequences. The following verbatim string is identical to the preceding one.
            string b = @ "\\programs\csharp\firstprogram.cs";

Click here for more  : C#.NET Language Basics


No comments:

Post a Comment