Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday 11 May 2015

USAGE OF VALUE DATA TYPES IN C#


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

Click imaginationhunt.blogspot to see latest Blogs
 

VALUE TYPE

A variable which holds actual values then that type of data types are value types. Value type are 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.

VALUE DATA TYPES

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).

For ex- The default values for value types can be understand by the following example
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

How to use these Data Type?
We will see all the 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

For ex- So, let's see how to use int data type.
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            int a = 9;
            int b = 7;
            int result = a + b;
            Console.WriteLine(result);
        }
    }
}

Output:
16
/*This is how we use int type.*/

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)
For ex-
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            bool a=new bool();
            if (a == true)
            {
                Console.WriteLine("result true");
            }
            else
            {
                Console.WriteLine("result false");
            }
        }
    }
}

Output:
result false 

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

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            char a = 'A';

            Console.WriteLine(a);
        }
    }
}

Output:
A

Click imaginationhunt.blogspot to see latest Blogs

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

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            //byte a = -5;  /*negative number are not in range*/
            //byte b = 3595;    /*larger number are not in range*/
            byte c = 255;
            //byte d = 256;    /*number greater than 255 are not in range*/
            Console.WriteLine(c);
        }
    }
}

Output:
255

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

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            short a = 25;
            Console.WriteLine(a);
        }
    }
}

Output:
25
 


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)
For ex-
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            float a = 25;
            Console.WriteLine(a);

            float b = 25.6f;
            Console.WriteLine(b);
        }
    }
}

Output:
25
25.6
 


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

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            double a = 25;
            Console.WriteLine(a);

            double b = 25.6;  /*fractional value are accepted without f, means without 25.6f*/
            Console.WriteLine(b);
        }
    }
}

Output:
25
25.6

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)
For ex-
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            long a = 25;
            Console.WriteLine(a);

            //long b = 25.6;   /*fractional value are not accepted*/
            //Console.WriteLine(b);
        }
    }
}

Output:
25

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

using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            DateTime presentdatetime = DateTime.Now;
            Console.WriteLine(presentdatetime);
        }
    }
}

Output:
5/10/2015 1:19:27 AM

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.

To read Usage of Reference Data Types in C# - Click here.

Related Questions:-

#newtoprgm try firstprogram

Q-1 Which of the following statement correctly assign a value 33 to a variable c?
byte a = 11, b = 22, c;

A) c = (byte) (a + b);
B) c = (byte) a + (byte) b;
C) c = (int) a + (int);
D) c = (int) (a + b);
E) c = a + b;
Ans- Option (A)

Q-2 Which of the following statements is correct?
short l = 20;
short m = 400;
int a;
a = l1 * l2;

A) A value 8000 will be assigned to a.
B) A negative value be assigned to a.
C) During arithmetic if the result exceeds the high or low value of the range, the value wraps around till the other side of the range.
D) An error is reported as widening conversion cannot takes place.
E) An overflow error will be reported since the result of the multiplication exceeds the range of a short integer.
Ans- Option(A).

Click imaginationhunt.blogspot to see latest Blogs

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...