-2

I have a very basic question about passing a value type by reference in c#. why c# doesn't allow us to pass a variable by reference if its not initialized? what will be the issue if we do it?

lets take for example below code

public static void main()
{
   int i;
   SomeMethod(ref i);
}

public static void SomeMethod(ref int i)
{
   i = 10;
}

why cant i execute this method? why compiler is stopping me from doing this? what issue will be faced if we pass a unassigned variable as reference? 
Md Yunus
  • 1
  • 5
  • Change `ref` to `out` if you want this kind of semantics. – Sweeper Jun 03 '21 at 07:36
  • @sweeper i want to understand the logic behind not allowing us to pass a unassigned variable as ref. its not about how i can change my code to make it work. – Md Yunus Jun 03 '21 at 07:38
  • Well you _can_ pass unassigned variables as reference. It's just not _called_ `ref`. It's not about "logic", but aesthetics. – Sweeper Jun 03 '21 at 07:39
  • Actually, you only have to initialize your value type when passing by-reference if it's a local variable. See first duplicate for why that is. Note that you only have to initialize it if you use `ref`. When using `out`, it becomes the _called_ method's responsibility to initialize it. See second duplicate. – Peter Duniho Jun 03 '21 at 07:40
  • @MdYunus If you could pass an uninitialized variable into a method using `ref`, how would the method determine if it is initialized or not? I'm not aware of any functionality to do this in C#. – ProgrammingLlama Jun 03 '21 at 07:41
  • 1
    Do you expect to be able to write `int i; Console.WriteLine(i);` - i.e. reading an unassigned local variable? If not, why would you expect to be able to do exactly the same (dangerous) thing simply by adding a level of indirection? – Jon Skeet Jun 03 '21 at 07:41
  • 2
    Note that this has nothing to do with `int` being a value type. You'd get exactly the same behavior for any other type - a variable *must* be definitely assigned before being used as a `ref` argument. – Jon Skeet Jun 03 '21 at 07:42
  • 1
    `what issue will be faced if we pass a unassigned variable as reference?` It won't compile. As you have discovered. ;) `want to understand the logic behind not allowing us to pass a unassigned variable as ref.` Because the people who wrote the compiler are trying to protect you from shooting yourself in the foot. If they allow it, they need to document what will occur etc etc, and live with the inevitable bug reports when it doesn't act the way people expect. If they _don't_ allow it - well, they likely protect some feet and reduce bug reports. – mjwills Jun 03 '21 at 07:43

3 Answers3

1

When you pass a variable using ref, you are asserting that the variable is already initialized (even if that value is null for reference types). It's no different to declaring it as a local variable and then trying to use it in the same method without initializing it. This is why you can't pass uninitialized variables. There's also the problem that, even if this were possible, you would have no means of telling whether or not it has been initialized in code.

As to why passing it doesn't simply initialize it to default(type), I couldn't possibly say. My feeling is that it makes sense that it doesn't, and that you should be explicit in the code you write, so initializing the variable beforehand makes sense.

So that you can use uninitialized variables, out exists. So to fix your code, replace ref with out. It's now the repsonsibility of SomeMethod to initialize i:

public static void main()
{
   int i;
   SomeMethod(out i);
}

public static void SomeMethod(out int i)
{
   i = 10;
}

Further reading:

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
1

If the variable is not initialized you use out instead. The check, that ref variables need to be initialized, is there to help you avoid programming errors

mortb
  • 9,361
  • 3
  • 26
  • 44
0

use out:

public static void main()
{
   int i;
   SomeMethod(out i);
}

public static void SomeMethod(out int i)
{
   i = 10;
}
Kazys
  • 377
  • 2
  • 12
  • i want to understand why its not allowed. its just a code example and i know how out and ref works. but i want to understand the internals of ref, my question is why, why is it not allowed. – Md Yunus Jun 03 '21 at 07:40
  • `why, why is it not allowed.` Because the compiler writers deemed it so. See the duplicate links. As it says there `Because experience has shown that forgetting to assign a value to a local is probably a bug.` – mjwills Jun 03 '21 at 07:46