Value Type and Reference Types

1.What are Value Types ? Give example
Variables that store data are called value types. Value types are stored on stack.
They contain the actual values. eg-int, enum, structs.  
2.What are Reference Types? Give Example.
Variables that store reference to actual data are called Reference types.Reference types
stored on heap but contain the address on heap.
eg-class,interface,delegate,string,object, Array

3.Difference between value type and reference types ?
   
Value Type
Reference Type
They are stored on stack
They are stored on heap
Contains actual value
Contains reference to a value
Cannot contain null values. However this can be achieved by nullable types
Can contain null values.
Value type is popped on its own from stack when they go out of scope.
Required garbage collector to free memory.
Memory is allocated at compile time
 Memory is allocated at run time
   
4.Diff bt stack and heap
   
Stack
Heap
Values are stored on one another like a stack.
Values are stored in random order.like dumped into a huge space
Used for value type
Used for reference types




5. Whats the output of the following program ?
      1.       int x = new int();
                x = 20;
                int y = new int();
                y = x;       
                y = 30;    
//Line3   
                return x;


            Output--20
            At Line 3 only y value is changes. "x" has its own location on stack which is not 
            affected by this and hence retains is old values.

     2.       class Numbers
               {
                   public int MyValue;
               }


            
               Numbers x = new Numbers();
               x.MyValue = 3;
               Numbers y = new Numbers();
               y = x;
               y.MyValue = 4;
               Console.WriteLine( x.MyValue);
           
               Output- 4. Both x and y are pointing to the same location on the heap.       


6.What is boxing and unboxing ?
    Boxing-- Converting a value type to a reference type .This is implicit.
                     During boxing follo 3 things happen-
                     Memory is allocated on heap,
                     Value is copied from stack to heap,
                      Reference is updated to point t heap
                     eg-
                int i = 123;
		object o = i; 
                                here if you make i= 456; 
                                then o=123, //value does not change 
 
 Unboxing- Converting reference type to value type.
                     The value which is being unboxed has to be boxed first. 
                      eg- int j = (int) o;       

 
7.Are Value Type sealed ?
   Yes                


8.Can you implement IDisposable for value type ?
Structs being a value type can inherit IDisposable interface

10.how do you get size of value type ?
using SizeOf operator.


11.When you declare int i,j hos is it alocated?
Both of them are allocated on the stack at the same time and they go out of scope at the 
same time.

12.What do you mean by Pass By Value ? 
Actual value is copied to another variable  eg-
              calling method--                    Actual Method      
              int a=4,                                    Add(int a,int b){
              int b=5,                                     a=6,b=7 }
             Add(4,5)
             Print a and b --Line 1
Even though the value of a and b is changed in the actualfunction it does not  chane in 
line 1.              
        
13.What do you mean by Pass By Reference?
The calling functions passes the address of the variable instead of the actual values.Any 
changes made inside the actual method will be reflected in the calling function.
 eg-
              calling method--                    Actual Method      
              int a=4,                                    Add(ref int a,ref int b){
              int b=5,                                     a=6,b=7 }
             Add(ref 4,ref 5)
             Print a and b --Line 1

The value at Line 1 willl be a=6,b=7    


13.What do you mean by Pass By out?
This is similar to pass by ref. Only diff is before passing the values they need not be 
initialized.But they have to be initialized inside actual method.They are mailny used in remoting.
eg-
              calling method--                    Actual Method      
              int a,                                       Add(out int a,out int b){
              int b,                                       a=6,b=7 }
             Add(out 4,out5)
             Print a and b --Line 1
 The value at Line 1 willl be a=6,b=7  

      
14.what are params ? 
They are used when you do not know how many variables you will be passing across methods.
eg-
           calling method--                     Actual Method      
           method(1,2,3,4)                      method(int a,params int[])   
The param array should be last while declaring method arguments.       
  
15.What the difficult parameter type used when calling methods in c# ?
Pass by Value
       
13.What are diff types of parameter in c# ?
Value parameter(in), Reference/ inout paramete  ref),Output parameter(out),  
Parameter  Array(param)


   
     






23 comments:

  1. Thanks for the article, Great work!

    The statement:
    "Reference types stored on "heap" but contain the address on "heap".

    may not be accurate: the address is stored on the stack, which points to a memory location on the heap.

    ReplyDelete
  2. Replies
    1. 30. ......…….are a kind of parameterized data structure that can work with value types as well as reference types.

      Delete
  3. Some more selected ... http://net-informations.com/faq/default.htm c# interview question and answers.

    Makki

    ReplyDelete
  4. 30. ......…….are a kind of parameterized data structure that can work with value types as well as reference types.

    ReplyDelete
  5. WHAT is line-1 means in q.12

    ReplyDelete
  6. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.
    Dot Net Training in chennai |dot net training institute in velachery

    ReplyDelete


  7. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training and placement in chennai | top10 dot net training institutes in chennai

    ReplyDelete
  8. Learn how to create application using C# programming language in hindi/urdu with complete notes of all lectures regarding C#. C# programming tutorial with OOP(Object-oriented Programming) concepts in easy hindi/urdu (basic to advance level).

    Please Visit: http://www.csharp-roman-urdu.blogspot.com

    ReplyDelete
  9. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to my career. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things
    dot net training in velachery | dot net training in chennai

    ReplyDelete
  10. The dotnet interview questions shared are very much useful My sincere thanks for sharing this post and please continue to share this post
    Dot Net Training in Chennai

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Great post. This article is really very interesting and enjoyable. I think its must be helpful and informative for us. Thanks for sharing your nice post.
    dotnet training in chennai

    ReplyDelete
  13. Thanks for sharing use interview questions on .Net technology. While preparing for my job interview, your article helped me a lot to sharpen my skills and do well in my interview.
    Big data Analytics Training in Chennai | Software Testing Training in Chennai

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. "I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
    "

    ReplyDelete