jyotishka bora
jyotishka bora

Reputation: 3973

How to downcast a ref variable within the method

I need to downcast a long to an int in a method where the long is passed as a ref variable:

public void Foo(ref long l)
{
    // need to consume l as an int
}

How can I easily do this?

Upvotes: 0

Views: 554

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500873

You can't. However, any value you want to put into a ref int can be put into a ref long anyway - you've just got to worry about the initial value, and what you want to do if it's outside the range of int.

How many places do you need to write to the ref parameter or read it within your code? If it's only in one or two places, you should be okay just to cast appropriately at the right times. Otherwise, you might want to introduce a new method:

public void Foo(ref int x)
{
    // Here's the body I *really* want
}

public void Foo(ref long x)
{
    // But I'm forced to use this signature for whatever
    // reasons. Oh well. This hack isn't an *exact* mimic
    // of ref behaviour, but it's close.

    // TODO: Decide an overflow policy
    int tmp = (int) x;
    Foo(ref tmp);
    x = tmp;
}

The reason I say in the comments that it's not an exact mimic for the behaviour is that normally changes to the original ref parameter are visible even before the method returns, but now they'll only be visible at the very end. Also, if the method throws an exception, the value won't have been changed. The latter could be fixed with try/finally, but that's a bit clunky. In fact, if you want the try/finally behaviour you can do it all in a single method easily:

public void Foo(ref long x)
{
    int y = (int) x;
    try
    {
        // Main body of code
    }
    finally
    {
        x = y;
    }
}

Upvotes: 5

Eoin Campbell
Eoin Campbell

Reputation: 44278

You can't safely cast a long to an int regardless of whether it's nullable or not as theres a chance it will overflow.

try this

        if (!blah.HasValue)
            blah = long.MaxValue;

        int x = (int)blah.Value;

        Console.WriteLine(x); //Not What you expect

Upvotes: 1

Michael Meadows
Michael Meadows

Reputation: 28416

You're a little light on the details, but if you're talking about this scenario:

public void Something(ref long something)
{
    // code
}

int foo;
Something(ref foo);

try this:

long foo;
Something(ref foo);
int bar = (int) foo;

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564441

You cannot directly cast this. The best option would be to cast it to a local, then assign it at the end of your method.

void Method(ref long myValue)
{
   int tempValue = (int)myValue;
   // change tempValue
   myValue = tempValue;
}

Upvotes: 0

mqp
mqp

Reputation: 71945

You don't. You can't take your reference and point it to a different type. How would the code calling your method know that it's changed?

If you just want to work with the value as an int, then you could do something like this:

private void Process(ref long l)
{
    int i = (int)l;
    // do whatever
}

Upvotes: 3

Related Questions