Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - DATA TYPES


We have seen that a variable is used to hold a value. Be it a number or a name or even a sentence.


i.e.


int x = 5

Or


string y = "Hello World"

But how will C# come to know, if the value is a number or a name?


And thus Data Types comes into picture.


Data Types


Data Types are the hidden gem that is used by C# to determine if the variable is going to hold a number or a name.


But why does C# need to determine if the variable it is going to hold be a number or a name? Can it not create one type of variable and store all the values there.


It cannot do that. Because if you want to store a number. It would take less space.

C_Sharp

On the other hand, if you want to store a String like Hello World. It would take a larger space.

C_Sharp

So, whenever you want to create a string variable and assign a string to a variable.


string y = "Hello World"

And C# finds out that it is a String type value and creates the storage space accordingly.

C_Sharp

The above Data Type for String is called string Data Type.


And similarly for a number, for a decimal value e.t.c. C# has different Data Types.


C# Data Types are categorised as :

  1. String

  2. Character

  3. Number

  4. Boolean

  5. Array


Data Types used in C#

  1. String



    As we have seen in the above example, a string is a set of letters/alphabets.

    The Data Type for String is string.

    string y = "Hello World";


    The value assigned to a string type variable must be in Double Quotes (i.e. "").

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		string x = "Hello World";
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      Hello World


    In the above code we have initialised the variable x with the String Hello World.

    string x = "Hello World";


    Now, the string data type before the variable x will make sure that no values other than String would be allowed in it.

  2. Character



    In the above example i.e. String, we have seen how can a String variable hold a set of letters/alphabets.

    Now, what if we need a variable that would just hold one letter/alphabet.

    And yes, C# provides that as well. i.e. A Char Data Type would hold a letter/alphabet.

    The only thing you need to keep in mind is, place the letter/alphabet inside single quotes ('').

    Let us see in the below example,

    Example :



    public class MyApplication
    {
    	public static void Main(string[] args)
    	{
    		char x = 'Y';
    		System.Console.WriteLine(x);
    	}
    }
    


    Output :



      Y


    In the above code we have initialised the variable x with the letter/alphabet Y.

    char x = 'Y';


    Now, the char data type before the variable x will make sure that no values other than letter/alphabet would be allowed in it.

    A char data type takes 2 bytes of memory.

  3. Integers



    Integers are the whole numbers (i.e. 7 or 5 or 2.14).

    Below are the data types used for Integers.

    1. int Data Type



      The Data Type for a whole number is int.

      The Int data type is used to store whole numbers from -2147483648 to 2147483647. And takes 4 bytes of memory.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		int x = -34535;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        -34535

    2. uint Data Type



      The Data Type for a whole number is uint.

      The Int data type is used to store whole numbers from 0 to 4,294,967,295. And takes 4 bytes of memory.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		uint x = 34535;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        34535

    3. long Data Type



      Say, we have a large value 3372036854775808.

      Well! In this case int data type will not be able to hold that value.

      And in this scenario, we need a long data type.

      A long data type is used to store whole numbers from -9223372036854775808 to 9223372036854775808. And takes 8 bytes of memory.

      For example 3372036854775808.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		long x = 3372036854775808;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        3372036854775808


      So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.
      Note : You can also use suffix L with the value 3372036854775808, i.e. 3372036854775808L to denote the value is of long data type. However, it is completely optional.

    4. ulong Data Type



      A ulong data type is used to store whole numbers from 0 to 18,446,744,073,709,551,615. And takes 8 bytes of memory.

      For example 3372036854775808.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		long x = 3372036854775808;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        3372036854775808

    5. byte Data Type



      The Byte Data Type is used to hold whole numbers between 0 to 255.

      Say, there is a small number i.e. 5. So, to save space we can use Byte Data Type.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		byte x = 5;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        5

    6. sbyte Data Type



      The sbyte Data Type is used to hold numbers between -128 to 127.

      Say, there is a small number i.e. -5. So, to save space we can use Byte Data Type.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		sbyte x = -5;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        -5

    7. short Data Type



      The short Data Type is used to hold whole numbers between -32768 to 32767.

      Say, there is a number i.e. 2768. So, to save space we can use Short Data Type.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		short x = -2768;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        -2768

      Note : We can still use int with numbers 5 and 2768.

    8. ushort Data Type



      The ushort Data Type is used to hold whole numbers between 0 to 65,535.

      Say, there is a number i.e. 2768. So, to save space we can use ushort Data Type.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		ushort x = 2768;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        2768

    9. Float Data Type



      A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 32 bit.

      For example 5.987.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		float x = 5.987F;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        5.987


      So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.

      float x = 5.987F;


      Just note, we have mentioned 5.987F instead of the number 5.987. This is an additional step you need to write to tell C# that 5.987 is a Floating point number.

    10. double Data Type



      A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.

      It is almost same as Float data type. Just that it can hold a floating point number of a larger size.

      For example 5.987434343434343434343.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		double x = 5.98743486215732D;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        5.98743486215732


      So, in the above example, we have created a variable x of type Double. And assigned the value 5.98743486215732 to it.

      double x = 5.98743486215732D;

  4. Booleans



    Boolean represents just two values, i.e. true or false.

    1. bool Data Type



      A bool Data Type can accept only true or false.

      Example :



      public class MyApplication
      {
      	public static void Main(string[] args)
      	{
      		bool x = true;
      		System.Console.WriteLine(x);
      	}
      }
      


      Output :



        true