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)


   
     






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


  6. 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
  7. 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
  8. 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
  9. 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
  10. This comment has been removed by the author.

    ReplyDelete
  11. 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
  12. 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
  13. This comment has been removed by the author.

    ReplyDelete
  14. "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
  15. This comment has been removed by the author.

    ReplyDelete
  16. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  17. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  18. After reading your article I was amazed. I know that you explain it very well. And I hope that other Python classes in pune readers will also experience how I feel after reading your article.

    ReplyDelete
  19. Thanks for sharing the updating information

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course in Hyderabad ­

    ReplyDelete
  20. This is very good content you share on this blog. it's very informative and provide me future related information!!
    Artificial Intelligence Course

    ReplyDelete
  21. You can make C# Nullable Types such as int and double to allow null to be their values. Null values are only storable to reference types like string and other objects.

    ReplyDelete

  22. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging! devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery




    ReplyDelete
  23. Cool stuff you have and you keep overhaul every one of us
    learn360digitmg artificial intelligence online course

    ReplyDelete
  24. I feel a lot more people need to read this, very good info! .
    360digitmg data science courses online

    ReplyDelete
  25. It is extremely nice to see the greatest details presented in an easy and understanding manner.
    data scientist certification malaysia

    ReplyDelete
  26. Supradyn Tablet is use in very easy in people. this tablet is multivitamin

    ReplyDelete
  27. this is really nice to read..informative post is very good to read..thanks a lot!
    data scientist training in malaysia

    ReplyDelete
  28. Nice blog. Informative and knowledgeable content. Big thumbs up for this blog. I really enjoyed this blog. Thank you for sharing with us.
    Data Science Course Training Institute in Hyderabad with Placements

    ReplyDelete
  29. Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine. data science course in surat

    ReplyDelete
  30. Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long.
    I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share.
    I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    data scientist training in hyderabad

    ReplyDelete
  31. Veri Informative Blog... keep it up. Thanks For Sharing.

    Personal loan for CIBIL defaulter

    ReplyDelete
  32. True eye seat ability pretty environment major. Billion rich after. Decade eye force significant hotel institution plant.education

    ReplyDelete