Variables,Constant And Checked,Unchecked Keyword

In this article i going to explain variable, constants used in c#. Also i am going to explain what is the difference between int and Int32 and string and String. In this article we will able to know what is the difference between checked and unchecked keyword.

Data Type: – it means a particular kind of data item or value stored in that type.  Data present on hard disk mean text files, images, songs have a different format but computer knows only 1 & 0 so data internally managed in binary format.

Variable: – Is a name which is given to storage location in memory.

Before using that variable you need to assigned value to that variable else it throws compile time error.

Constant: – Is immutable type means the value of that constant not changed throughout the program and you can’t modify at runtime.

Primitive Type: –

  1. Integer Type: – byte,short,int,long.
  2. Real Numbers: – float,double,decimal.
  3. Character: – char.
  4. Boolean: – bool.
  5. Byte: store value of 8-bit length and value from 0-255 ex byte b=’75’; 2^8=256
  6. Char- 2-byte length data type and contains Unicode data ex char v=’k’; 2^16 = 65536
  7. What is the difference between byte and char?
  8. A byte is of 8 bit and max value 256 i.e. 0-255
  9. Char contains 2 bytes means to represent Sanskrit char like for that use
  10. String- series of char data
  11. Numeric type –  signed numbers mean + as well as – value.
    1. Numbers with decimal
    2. Numbers without decimal
      1. Int16- 16 bit signed numeric
      2. Int/Int32 – 32 bit signed numeric
      3. 64 bit signed numeric –Int64

 What is the difference between int and Int32, string and String?

There is no difference they are only alias. All the methods available for int and Int32 and string and String are the same. But only namespaces are different. Unsigned means only positive numbers.

Non Primitive Types: String, class, enum, array, delegate, array or we can say user-defined data type.

Checked and Unchecked Keyword:

The checked keyword is used for when you push right-hand size data to left-hand side without considering the threshold value or it’s limit then unexpected result comes so that’s why a Checked keyword is used.

Variables,Constant And Checked,Unchecked Keyword

Use of Checked Keyword means you don’t want to push data beyond Its limit or left-hand side data type then it is used checked keyword.

At runtime, if overflow happens it will throw an error which is shown below so you can use try catch block to handle that error.

Variables,Constant And Checked,Unchecked Keyword

Unchecked Keyword:  Is the same behavior of default means it doesn’t write any things it will work the same.

So why we need that keyword of its behavior same as default because for some data type compiler check the overflow at compile time so bypass this behavior we used the unchecked keyword.

Variables,Constant And Checked,Unchecked Keyword

If used UnChecked keyword then no compile-time checking is done.

Variables,Constant And Checked,Unchecked Keyword

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *