Data Types
In C#, data types are used to store values in the memory. When data type is declared, it allocates a block of memory to hold the value. All the data types are directly or indirectly derived from the
System.object
class.
Before moving, we need to understand the variables.
Variables
A variable is used to store the value (data). It is an address given to a memory location.
Example:
Take an example of a bag here bag is a variable we can put books into a bag and we can take them out again but we can put books according to bag size, a bigger bag can hold more books. Similarly, we can store and retrieve data from a variable.
Here bag is the variable name the size of bag is the data type, an
int
can hold 32 bit value and long
can hold 64 bit value. Different data types have different sizes we use them according to our requirement for example if we want to store only two values true
or false
we use bool
.Checking Size of a Data Type
You don’t need to remember all the sizes because there is method
sizeof()
through which we can easily check the size of data type which is given in bytes.
Note by this method (
sizeof
) you can check only the sizes of value types, you cannot check the size of a reference type. For example, you cannot check the size of a string
.
Like this below, you can check any value type size in bytes:
Console.WriteLine(sizeof(int));//checking the size of int
Console.ReadKey();
Checking the Data Type
We can check the one data type belongs to which class by a method
GetType()
like below:// to check the type of an data type
int a = 55;
Console.WriteLine("{0}", a.GetType());
Console.ReadKey();
Checking the Two Objects Have the Same Type It Will Return True Otherwise False
Suppose we have two object
obj1
and obj2
of the same type long
then we can get the true
by using Object.ReferenceEquals(x.GetType(),y.GetType())
like below:long obj1 = 55;
long obj2 = 3232;
float obj3 = 23;
Console.WriteLine("obj1 and obj2 are of same type: " +
Object.ReferenceEquals(obj1.GetType(), obj2.GetType()));
Console.WriteLine("obj1 and obj3 are of different type: " +
Object.ReferenceEquals(obj1.GetType(), obj3.GetType()));
Console.ReadKey();
//// OUTPUT/////
//obj1 and obj2 are of same type: True
//obj1 and obj3 are of different type: False
Declaring a Variable and Initializing a Variable
C# is a strongly typed language means all variables must be declared and initialized before use. Once a variable type is declared, it cannot be changed. Behavior of the data type is declared by its type if you use
int
, it can hold only integers for fraction values use decimal etc. to change the behavior of a variable it needs to be converted.Declaring a Variable
To use a variable, we need to declare them first like below:
<data type> <variable name>;
Int a;
Initializing a Variable
A variable is initialized by an equal sign operator (=) and writing the value on the right side of equal sign like below:
<data type> <variable name>=value;
Int a=55;
int a;//declaring variable a
a = 55;//initializing a
int a=55;//declaring and initializing in one line
// declaring multiple variable of same time in one line
int a, b, temp1;
string abc, mainButton, _button;
Variable Naming Convention
In .NET, there are two types of naming convention:
camelCase
in this we write first letter with lower case and second word with starting first letter in upper case, e.g.,firstName, lastName
PascalCase
in this we write first letter with upper case and second word with starting first letter in upper case, e.g.,FirstName, LastName
Mostly, variable names are written by using
camelCase
naming convention. But it’s not mandatory, it is just a professional way of writing names like this:- The first character of a variable name must start with a letter or underscore (
_variableName
) or with at sign@
. - C# reserve keywords like
using
,namespace
,class
cannot be used as variable names - There should not be a space between variable names, subsequent characters can have numbers, underscores, and letters.
If you don’t follow the above rules, then the compiler will never follow your rules, it will give errors.
Some Variable Names Which Are Legal
int abc;
long _abcd;
float @abcd;
bool main_button;
decimal piValue;
string firstName;
string first_name;
bool button55_on;
Some Example of Variables Which Are Illegal
int 5abc;
long _a.5bc5d;
float @ab cd;
decimal pi@Value;
//c# reserve keyword cannot be used as variable namebool class;
string namespace;
string string;
int static;
Note: For more details of C# reserved keywords, visit https://msdn.microsoft.com/en-us/library/x53a06bb.aspx.
Related Topics:
1 comments :
Click here for commentsHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.
Conversion Conversion Emoticon Emoticon