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: –
- Integer Type: – byte,short,int,long.
- Real Numbers: – float,double,decimal.
- Character: – char.
- Boolean: – bool.
- Byte: store value of 8-bit length and value from 0-255 ex byte b=’75’; 2^8=256
- Char- 2-byte length data type and contains Unicode data ex char v=’k’; 2^16 = 65536
- What is the difference between byte and char?
- A byte is of 8 bit and max value 256 i.e. 0-255
- Char contains 2 bytes means to represent Sanskrit char like for that use
- String- series of char data
- Numeric type – signed numbers mean + as well as – value.
- Numbers with decimal
- Numbers without decimal
- Int16- 16 bit signed numeric
- Int/Int32 – 32 bit signed numeric
- 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.
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.
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.
If used UnChecked keyword then no compile-time checking is done.