Datatype and keyword

Like any programming language, C# defines keywords for fundamental data types, which are used to represent local variables, class data member variables, method return values, and parameters. Unlike other programming languages, however, these keywords are much more than simple compiler- recognized tokens. Rather, the C# data type keywords are actually shorthand notations for full-blown types in the System namespace.

Table lists each system data type, its range, the corresponding C# keyword, and the type’s compliance with the common language specification (CLS).
C# keyword System type Range Meaning CLS complaint?
bool System.Boolean true or false Represents truth or falsity Yes
sbyte System.SByte -128 to 127 signed 8-bit number No
byte System.Byte 0 to 255 Unsigned 8-bit number Yes
short System.Int16 -32768 to 32767 signed 16-bit number Yes
ushort System.UInt16 0 to 65535 Unsigned 16-bit number No
int System.Int32 –2,147,483,648 to 2,147,483,647 Signed 32-bit number Yes
uint System.UInt32 0 to 4,294,967,295 Unsigned 32-bit number No
long System.Int64 –9,223,372,036,854,775, 808 to 9,223,372,036,854,775,807 Signed 64-bit to number Yes
ulong System.UInt64 0 to 18,446,744,073,709,551,615 Unsigned 64-bit number No
char System.Char U+0000 to U+ffff Single 16-bit Unicode character Yes
float System.Single -3.4 1038 to +3.4 1038 32-bit floating-point number Yes
double System.Double ±5.0 10–324 to ±1.7 10308 64-bit floating-point number Yes
decimal System.Decimal (-7.9 x 1028 to 7.9 x 1028)/(100 to 28) 128-bit signed number Yes
string System.String Limited by system memory Represents a set of Unicode characters Yes
object System.Object Can store any data type in an object variable The base class of all types in the .NET universe Yes
CLS-compliant .NET code can be used by any managed programming language. If you expose non–CLS-compliant data from your programs, other .NET languages might not be able to make use of it.

Variable declaration and initialization

When you are declaring a local variable (e.g., a variable within a member scope), you do so by specifying the data type followed by the variable’s name.

Syntax


datatype var_name;
  

Example


static void  Localvariable()
{
    int MyInt;
    double MyDouble;
    string MyString;
}
  
Be aware that it is a compiler error to make use of a local variable before assigning an initial value. Given this, it is good practice to assign an initial value to your local data points at the time of declaration. You may do so on a single line

static void  Localvariable()
{
    //datatype varName =  initialValue
    int MyInt = 0;
    double MyDouble = 10;
    string MyString = "my string";
}
  
or by separating the declaration and assignment into two code statements.

static void  Localvariable()
{
    //datatype varName;
    //varName = initialValue;
    int MyInt;
    Myint = 0;
    string MyString;
    MyString = "my string";
}

It is also permissible to declare multiple variables of the same underlying type on a single line of code, as in the following three bool variables:

static void  Localvariable()
{
    bool b1 = true, b2 = false, b3 = false;
}

Since the C# bool keyword is simply a shorthand notation for the System.Boolean structure, it is also possible to allocate any data type using its full name (of course, the same point holds true for any C# data type keyword). Here is the final implementation, which illustrates various ways to declare a local variable:

static void  Localvariable()
{
    System.Boolean b1 = true;
    System.String str = "system string";
}   

Example program of local variable

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        Localvariable();
        Console.ReadLine();
    }

    static void  Localvariable()
    {
        //datatype varName
        int MyInt;
        double MyDouble;
        string MyString;
        //varName = initialValue
        MyInt = 5;
        MyDouble = 30;
        MyString = "my string";
        System.Boolean b1 = true;
        System.String str = "system string";
        Console.WriteLine("int: {0}",MyInt);
        Console.WriteLine("bool: {0}", b1);
        Console.WriteLine("string: {0}",str);
    }
}
  
  

Output


int: 5
bool: True
string: system string
  

Intrinsic datatype and the new operator

All intrinsic data types support what is known as a default constructor. This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value.

bool variables are set to false.
  • Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
  • char variables are set to a single empty character.
  • Object references (including strings) are set to null.
It is more cumbersome to use the new keyword when creating a basic data type variable.

Program


using System;

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        WithNew();
        Console.ReadLine();
    }

    static void  WithNew()
    {
        //create variable using new
        bool b = new bool();
        Console.WriteLine(b);
        int i = new int();
        Console.WriteLine(i);
        object o = new object();
        Console.WriteLine(o);
    }
}
  
  

Output


False
0
System.Object
  

Datatype class hierarchy

It is interesting to note that even the primitive .NET data types are arranged in a class hierarchy. The relationship between these core system types can be understood with below image: This hierarchy based on inhatitance concept. If you are new to inheritance see the inharitance article of c#.
Notice that each type ultimately derives from System.Object.

Comments

  1. Thanks for sharing valuable information. Your blogs were helpful to Dot NET learners. I request to update the blog through step-by-step. Also, find the Dot net news at Dot NET Online Training

    ReplyDelete

Post a Comment

Popular posts from this blog

System.Environment

System.Console