DATA TYPES IN C#

Today in Engineer's World, we are discussing a very important topic of C# - Data Types in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 

DATA TYPES

Data Type in a programming language describes that what type of data a variable can hold. The type of data that a variable contains is called Data Type.
C# is a Strongly type typed language so every variable and object must have a type.

C# DATA TYPES

1. Basic in-built or primitive or predefined:
Ex- byte, short, int, float, double, long, char, bool, DateTime, string, object,etc.

2. Non-primitive or User-defined:
Ex- enum, class, struct, interface, delegate, array.

The variables in C#, are categorized into the following types:
  • Value types
  • Reference types


VALUE TYPE:

A variable which holds the actual values then that type of data types are value types. Value type is stored in "stack" memory and these value types are fixed in size.
Value type variables are local copies. When they fall out of the scope they may die. Value types are derived from System.ValueType.
Ex- byte, short, int, float, double, long, char, bool, DateTime.
Things to Remember:
a) Primitive data type are value type except string.object.
b) struct, enum are value types.
c) The default value, if value is not assigned is 0 for value type(i.e. whether it may int, long, short, char). 

Click imaginationhunt.blogspot to see latest Blogs

Example- The default values for value types can be understand by the following example.

Practical Implementation 

using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            int a1 = new int();
            Console.WriteLine(a1);

            char a2 = new char();
            Console.WriteLine(a2);


            short a3 = new short();
            Console.WriteLine(a3);

        }
    }
}



Output

0 //for int default is 0.
   //for char it is ' '.
0 //for short it is 0.

Now, check out Figure 1. This is the most important chart or figure for understanding data types (types, namespace, size, low range of type, high range of type). Interviewer may also asked question like, tell the size in byte taken by decimal or does int variable can store a value 2145656565?
So, I strongly suggest you to look and read this chart to better under data type.

FIGURE 1


Check out all types one by one:

1. int: An integer is a number with no fractional part; it can be positive, negative or zero. It can store number up to a range that is mentioned in the figure. It represent a 32-bit signed integer.
Namespace & Assembly of type can be checked by Figure-2. All you need is place your mouse pointer on that data type and press F12. Then, you would transfer to its definition. For example- place you mouse pointer on int in your code and Press F12. You will see a similar page as shown in Figure-2.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

FIGURE 2

2. bool: Represent a boolean value. Boolean can have only two value it may be true or false.
By default value is false.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

3. char: To represent a single unicode character.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)


4. byte: It represent an 8-bit unsigned integer.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)


5. short: It represent an 16-bit signed integer.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

Click imaginationhunt.blogspot to see latest Blogs

6. float: It represent a single precision floating point number. Float is used to store integer and fractional numbers. But in case of fractional number 'f' would be add to its end.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)


7. double: It represent a double precision floating point number.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

8. long: It represent a 64-bit signed integer. 'long' is used to store large number as the range of this type is very large.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

9. DateTime: is used for store and retrieve time. 
Namespace -System.
Assembly- mscorlib (mscorlib.dll)
  
REFERENCE TYPE:

A variable holds a reference to the value, then that type of data type are reference types. These reference types are stored in "heap" memory and these types are not fixed in size. They are maintained by System managed heap, but it also uses stack to store a reference of the heap.
Two primitive types (string and object), and
Non-primitive data types (class, interface, delegates) are example of reference type.
Ex- class, interface, delegates, string, object and array.
Things to Remember:
a) Object type is superior to all types. It can store any type or any size of data. It helps in Inheritance process.

1. string: It represent text as a Unicode characters.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

2. array: Array can use with any type.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)

3. object: can store any type or any size of data.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)


In C# it is possible to convert a value of one type into a value of another type. The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.
When we declare a variable, we have to tell the compiler about what type of the data the variable can hold or which data type the variable belongs to.



Related Questions: 

#newtoprgm try firstprogram

Q-1 Which of the following are correct about data types?
1. If the integer literal exceeds the range of byte, a compilation error will occur.
2. We cannot implicitly convert non-literal numeric types of larger storage size to byte.
3. Byte cannot be implicitly converted to float.
4. A char can be implicitly converted to only int data type.
5. We can cast the integral character code.

A) 1, 3, 5
B) 2, 4
C) 3, 5
D) 1, 2, 5
Ans- Option(D).


2. Which of the following is an 8-byte integer?
A) Char
B) Long
C) Short
D) Byte
E) Integer
Ans- Option(B).

3. Which of the following is the correct ways to set a value 3.14 in a variable pi such that it cannot be modified?
A) float pi=3.14F;
B) #define pi 3.14F;
C) const float pi = 3.14F;
D) const float pi; pi= 3.14F;
E) pi = 3.14F; 
Ans- Option(C).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...












Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions