Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday 12 May 2015

USAGES OF REFERENCE DATA TYPES IN C#


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

Click imaginationhunt.blogspot to see latest Blogs
 

A variable holds a reference to the value, then that type of data type is reference types. These reference types are stored in "heap" memory and these types are not fixed in size. They are maintained in System managed heap but it also uses stack to store reference of the heap. 

Two primitive types (string and object), and
Non-primitive data types (class, interface, delegates) are example of reference type.

REFERENCE DATA TYPES

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)

For ex- 
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            string str1 = "Engineer's";
   //str1 store "Enginner's" which is nothing but text 
            string str2 = "World";
    //str2 store "World" which is nothing but text
            string result = str1 + str2;
            Console.WriteLine(result);
        }
    }
}

Output:
Engineer'sWorld


2. array: Array can use with any type.
Namespace -System.
Assembly- mscorlib (mscorlib.dll)
For ex- Here we have used for loop to understand it better.
using System;

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {           
            int[] numbers={1,2,3,4,5,6,7,8,9,10};
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine(numbers[i]);
            }
        }
    }
}

Output:
1
2
3
4
5
6
7
8
9
10

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

namespace AspnetSolutions
{
    class Dtype
    {
        public static void Main()
        {
            int a = 15;
            object obj = a;
            object obj1 = 150;
            object obj2 = "Hello";

            Console.WriteLine("a= {0}", a);
            Console.WriteLine(a.GetType());
            Console.WriteLine("obj= {0}", obj);
            Console.WriteLine(obj.GetType());
            Console.WriteLine("obj1= {0}", obj1);
            Console.WriteLine(obj1.GetType());
            Console.WriteLine("obj2= {0}", obj2);
            Console.WriteLine(obj2.GetType());
        }
    }
}
Output:
a= 15
System.Int32
obj= 15
System.Int32
obj1= 150
System.Int32
obj2= Hello
System.String

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 Boxing and Unboxing in C# - Click here.

Related Questions:-

#newtoprgm try firstprogram

Q-1 Which of the following statements are correct about c# datatypes?
1. Every data type is either a value type or a reference type.
2. Value types are always created on the heap.
3. Reference types are always created on the stack.
4. Mapping of every value type to a type in CTS facilitates Interoperability in C#.
5. Every reference type gets mapped to a type in CTS.
A) 1, 3
B) 2, 5
C) 1, 4
D) 3, 4
Ans- Option(C).

Q-2 Which of the following statements are correct?
1. We can assign values of any type to variables of type object.
2. When a variable of a value type is converted to object, it is said to be unboxed.
3. When a variable of a object type is converted to value, it is said to be boxed.
4. Boolean variables cannot have a value of null.
5. When a value type is boxed, an entirely new object must be allocated and constructed.

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

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