开发者

Passing an explicit cast as a ref parameter (C#)

开发者 https://www.devze.com 2022-12-18 08:07 出处:网络
I have a class that is mostly a wrapper for a big array and some associated housekeeping.I have a function that takes a ref parameter.When I pass an instance of the class into the function, I want the

I have a class that is mostly a wrapper for a big array and some associated housekeeping. I have a function that takes a ref parameter. When I pass an instance of the class into the function, I want the array to get sent.

I considered explicit casts. Let's say I have some function that has a byte[] ref parameter.

    public void SomeFunction(ref byte[] someBytes);

And that I have some class with an overloaded explicit cast.

    class SomeClass
    {
        byte[] someBytes;
        public static explicit operator byte[](SomeClass someInstance)
        {
            return someInstance.someBytes;
        }
    }

Now I want to call the function with the class as a parameter

    SomeClass someInstance = new SomeClass();
    SomeFunction(ref (byte[]) someInstance);

The compiler complains "a ref or out argument must be an assignable variable". I'm not开发者_如何学Go sure if I'm just failing to massage the compiler properly or if you really just can't do that.

I considered a Property or function return value, but you can't pass those by ref (and after educating myself I see why...)

I'd prefer not to make the array a public field, but that does satisfy the compiler. I suppose I could just create a local variable to reference the array with, but that's an extra line of code before and after each function call...

EDIT: it might be worth noting that SomeFunction was written by a third party and I don't have access to change it. Worse, I don't think their parameter actually needs to be ref...


A cast is not an assignable variable; you're passing a return value from your explicit cast operator.

You can create a variable holding the properly cast value before passing it as a ref:

SomeClass someInstance = new SomeClass();
byte[] someBytes = (byte[])someInstance;
SomeFunction(ref someBytes);

Note that it is now the someBytes variable which may be reassigned. You will have to take action to reassign someInstance.someBytes in some way after the call to SomeFunction if you want the internal value of someInstance to be reassigned.


You can't do that, you need to materialize the result of casting your object into a variable first.

Consider the following code, if what you ask for was allowed:

public void SomeFunction(ref byte[] someBytes)
{
    someBytes = new byte[] { 1, 2, 3 };
}

SomeClass someInstance = new SomeClass();
SomeFunction(ref (byte[]) someInstance);
// uh-oh, what is "someInstance" now?

Why is the argument marked "ref", what problem is it that you're trying to solve here?


The scope of values that can be used as a ref parameter in C# is limited by what the CLR allows. It's specified in CLI spec at sections 12.4.1.5.2 and 12.1.6.1 and includes

  • Argument of the current method
  • Local variable
  • Member Field of an object
  • Static Field
  • Array Element

A cast does not fit any of these and hence cannot be used as ref value. You'll need to assign it to a value like a local before passing it by reference.


Do you really have to cast the ref parameter? In my case something like this works fine.

byte[] someInstance = (byte[])new SomeClass(); 
SomeFunction(ref someInstance);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号