Quiz: C# object assignment- By Reference or by Value?

Go down

Quiz: C# object assignment- By Reference or by Value?

Post by BIT0122-Amit on Sun May 29, 2011 5:24 pm

Hey guys

I just found something wonderful last night. I was having trouble understanding observer pattern in C# code, so I experimented and googled around a bit, and found this answer.

Answer to what exactly?

When we assign an object to another object, how are the values of those objects assigned? By Value? Or By reference?What about the objects? Do the refer to same object? Or they just refer to two objects?

Take this quiz here, and then I will give the answer. Please use Hide tag when answering. The usage of hide tag is like this:


Your answer here

Here is the Code. We have an AnyClass class.
 class AnyClass {
        public int State {
            get; set; }

And we have the main Program:
  class Program {

        static void Main(string[] args) {
            AnyClass variable1 = new AnyClass();
            variable1.State = 10;
            AnyClass variable2 = variable1;
            Console.WriteLine(variable2.State);//this will print 10
            variable1.State = 100;

            Console.WriteLine(variable2.State);//1. What will this print?

            variable2.State = 1000;
            Console.WriteLine(variable1.State);//2. What will this print?

            variable1 = null;
            Console.WriteLine(variable2.State);//3. What will this print?

            if(variable2!=null) {
                Console.WriteLine("Variable 2 not null");//4. will this be printed?
            else {
                Console.WriteLine("Variable 2 null");//4. or this?

            if(variable1 != null) {
                Console.WriteLine("Variable 1 not null"); //5. will this be printed?
            }else {
                Console.WriteLine("Variable 1 is null"); //5. or this?




Mohaimin answered all questions perfectly. Good work! (it was not necessary to answer number 5 Very Happy )

Last edited by BIT0122-Amit on Sun Sep 11, 2011 5:25 pm; edited 2 times in total

Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Re: Quiz: C# object assignment- By Reference or by Value?

Post by BIT0102-Mohaimin on Sun Sep 11, 2011 12:59 pm

Nice post.
I think:

1. 100
2. 1000
3. 1000
4. Variable 2 not null

1. Assignment by reference. So, obviously variable2 will have the same values as variable1.
2. Same as 1.
3. variable 1 is REASSIGNED to something. variable2 has nothing to do with this.
4. Same as 3.

Here is something similar: http://iitdu.forumsmotion.com/t1690-check-your-skill-java-reference

Last edited by BIT0102-Mohaimin on Sun Sep 11, 2011 1:02 pm; edited 1 time in total (Reason for editing : The hide tag was not working. Used spoiler insteed.)

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Back to top

Permissions in this forum:
You cannot reply to topics in this forum