locked
Why it is necessery to initialize local variable if it is passed as "ref" to a method ? RRS feed

  • Question

  • Hi,

    please see this code:

    class MainClass
    {
      public static void Main()
      {
        int x;
        int y = 6;
    
        refTest obj = new refTest();
        obj.test(ref x, y); //error: Use of unassigned local variable
      }
    }
    class refTest
    {
      public void test(ref int x1, int y1)
      {
        x1 = 10;
        y1 = 20;
      }
    }
    In Main method variable x is though uninitialized at compile time (as i know,it is uninitialized at compile time,if i am wrong please let me know) but at runtime, it is assigned with 0,then why such a problem ?

    Wednesday, November 3, 2010 1:28 AM

Answers

  • Derek Smyth, there are various outstanding books I recommend you to read, if you haven't. :)

    * CLR via C#  

    * Applied Microsoft .Net Framework Programming

     


    Eyal, Regards.

    commandbrowser.eyal7.net
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:47 AM
    Wednesday, November 3, 2010 4:47 PM
  • That makes no sense with a Value type like an integer. 

    Standard is by value, the ref makes only sense by reference types where by ref an extra reference is created (I call it always a little box) to pass the place of the original reference, so if is created a new reference type object which is meant to change the old reference of that object this can be done.

    Be aware that passing by reference is therefore slightly taking more processing and memory than passing by value.

    If you want to pass a value type as a reference type, than you have first to box it in an object 


    Success
    Cor
    • Edited by Cor Ligthert Wednesday, November 3, 2010 11:31 AM
    • Proposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:35 AM
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:46 AM
    Wednesday, November 3, 2010 10:21 AM
  • The code 'int x' doesn't actually cause the value to be instantiated to 0. C# only thinks that the variable x has been initialised it's actual value is null.

    Value-type variables are not initialized to null and C# doesn't think they have been initialized to anything.

    From a C# perspective, they are considered uninitialized until you initialize them, and therefore cannot be used until then.

    From an IL perspective, they are initialized to their default value, since C# sets the localsinit bit on every method.

    • Proposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:39 AM
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:46 AM
    Wednesday, November 3, 2010 12:38 PM

All replies

  • Try changing the declaration of test() to:

    public void test(out int x1, int y1)
    {
     x1 = 10;
     y1 = 20;
    }
    
    

    The out modifier removes C#'s requirement to assign a value to a variable before passing it to a method.


    --pogo (pat)
    Wednesday, November 3, 2010 2:02 AM
  • that is semantic error, you dont proceed with the compilation and to the runtime, so the compiler is stopping you according to semantic error.
    Alan-SY
    Wednesday, November 3, 2010 2:43 AM
  • Hello,

    From IL, and CLR perspective out and ref do exactly the same thing, so basically it's a decision you need to take, if you don't need to initialize the value beforehand just use out, otherwise use ref.

    The only one who treats them differently is the compiler, your choice.


    Eyal, Regards.

    commandbrowser.eyal7.net
    Wednesday, November 3, 2010 2:55 AM
  • Is there any special situation where we do not have to initialize(or we cannot initialize) any variable and ought to use out type parameter or vice-versa ?

     

    Wednesday, November 3, 2010 10:04 AM
  • That makes no sense with a Value type like an integer. 

    Standard is by value, the ref makes only sense by reference types where by ref an extra reference is created (I call it always a little box) to pass the place of the original reference, so if is created a new reference type object which is meant to change the old reference of that object this can be done.

    Be aware that passing by reference is therefore slightly taking more processing and memory than passing by value.

    If you want to pass a value type as a reference type, than you have first to box it in an object 


    Success
    Cor
    • Edited by Cor Ligthert Wednesday, November 3, 2010 11:31 AM
    • Proposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:35 AM
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:46 AM
    Wednesday, November 3, 2010 10:21 AM
  • Hi bootstrap1,

    You really need to get yourself a copy of the book CLR via C#, all the brilliant questions you've been asking lately are answered in that book.

    The code 'int x' doesn't actually cause the value to be instantiated to 0. C# only thinks that the variable x has been initialised it's actual value is null.

    The code int x is a shortcut to the following code.....

    Int32 x;    // x is null

    Like a reference type the variable x hasn't been fully created yet, the following is how you'd actually create the variable....

    Int32 x = new Int32();  // x is 0

    Now when you pass the integer by ref your passing a pointer to the value. The value needs to be boxed on the heap for that to happen. Since x value is null there is nothing to box, it's unassigned.

    It's a very clever catch by the compiler!

     


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    • Proposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:35 AM
    • Unproposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:37 AM
    Wednesday, November 3, 2010 10:48 AM
  • if you don't pass the value type by reference then changes made to the int by obj.Test() will not be visible in Main() afterwards

    Wednesday, November 3, 2010 10:52 AM
  • If you want to pass a value type by ref, than you have first to box it in an object

    Since when?

    like you do

    Where?

    Wednesday, November 3, 2010 11:22 AM
  • If you want to pass a value type by ref, than you have first to box it in an object

    Since when?

    like you do

    Where?

    You are right, I did mean pass a value type as a reference type, I've changed it in the text.

    The second one was a misreading (I corrected it later and have now removed it)

    Thanks,

     


    Success
    Cor
    Wednesday, November 3, 2010 11:32 AM
  • The code 'int x' doesn't actually cause the value to be instantiated to 0. C# only thinks that the variable x has been initialised it's actual value is null.

    Value-type variables are not initialized to null and C# doesn't think they have been initialized to anything.

    From a C# perspective, they are considered uninitialized until you initialize them, and therefore cannot be used until then.

    From an IL perspective, they are initialized to their default value, since C# sets the localsinit bit on every method.

    • Proposed as answer by Mike Dos Zhang Friday, November 5, 2010 7:39 AM
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:46 AM
    Wednesday, November 3, 2010 12:38 PM
  • Hi Louis.fr;

    Your right. "Value-type variables are not initialized to null and C# doesn't think they have been initialized to anything."

    :)

    And your right about value types not being boxed when passed by reference. A managed pointer is passed not an object reference.

    Thanks for correcting me.

     

    In all honesty I don't think it's too bad to think that value tyes that are not initialised are set to null, even if it is not 100% technically correct.

    As to the value types being boxed when passed by reference, well that's my own fault for believing Cor.

     

     


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    Wednesday, November 3, 2010 1:33 PM
  • In all honesty I don't think it's too bad to think that value tyes that are not initialised are set to null, even if it is not 100% technically correct.


    That leads to questions such as "Why can't I test if a variable is initialized? I could in VB with IsEmpty"

    Wednesday, November 3, 2010 1:52 PM
  • In all honesty I don't think it's too bad to think that value tyes that are not initialised are set to null, even if it is not 100% technically correct.


    That leads to questions such as "Why can't I test if a variable is initialized? I could in VB with IsEmpty"


    Hey again Louis, going to retract the 'I don't think it's too bad to think that value types that are not initialised are set to null'. Thinking on it some more the statement is a contradiction. It's like saying variables that are not initialised are initialised; to null. Not initialised != Initialised.

    I'm amazed this hasn't cropped up in real life code, well it probably does all the time but it's an automatic pilot fix.

    Three things are sure.... bootstrap1 is asking great questions... your very knowledgable....  and .... I've still got some stuff to learn about C# and the CLR, and this has to be classed as 'the basics'.

     

    This would make a great interview question. I'm away to learn MSIL now.


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    Wednesday, November 3, 2010 3:31 PM
  • Derek Smyth, there are various outstanding books I recommend you to read, if you haven't. :)

    * CLR via C#  

    * Applied Microsoft .Net Framework Programming

     


    Eyal, Regards.

    commandbrowser.eyal7.net
    • Marked as answer by Mike Dos Zhang Sunday, November 14, 2010 7:47 AM
    Wednesday, November 3, 2010 4:47 PM
  • Hey Eyal, thanks for those. I have read CLR via C# Second Edition and I have the Third Edition sitting to be read. It's just trying to remember it all and the other 1 million software things that's going on.... honestly I need a more challenging job. I'm forgetting stuff like this because I'm not using it.....

    Anyway ....

    a quick crash course in IL will see me all right.


    …we each have more potential than we might ever presume to guess. (Blog: http://dsmyth.blogspot.com/)
    Wednesday, November 3, 2010 5:07 PM
  • Alright. :)

    Eyal, Regards.

    commandbrowser.eyal7.net
    Wednesday, November 3, 2010 5:41 PM
  • Hi bootstrap1,

    You really need to get yourself a copy of the book CLR via C#, all the brilliant questions you've been asking lately are answered in that book.

    Can you please name some of the books that will help me.I want to know how things are working in .NET Framework.
    Thursday, November 4, 2010 1:48 PM
  • Can you please name some of the books that will help me.I want to know how things are working in .NET Framework.

    I already pointed out two of them. :)


    Eyal, Regards.

    commandbrowser.eyal7.net
    Thursday, November 4, 2010 2:16 PM
  • Hi bootstrap1,

    Welcome to MSDN Forums!

     

    If your question has been solved, please don’t forget mark/vote the replies which helped you, and this will encourage the other community members to join in discussion and help each one.

     

    If there’s anything unclear, please feel free to let me know.

     

    Have a nice day!

    Mike

    *****************************************************

    [All-In-One Code Framework]

    Sample world! You will get more from this world!

    Welcome to the new world!

    Friday, November 5, 2010 7:40 AM