C# 传递类作为引用,必须声明主体,因为它没有标记为抽象、外部或部分。

huangapple go评论144阅读模式
英文:

C# Passing class by reference, must declare a body because it is not marked abstract, extern, or partial

问题

'Joystick.alStage()'必须声明一个主体,因为它没有标记为抽象、外部或部分。

英文:

I get this error when I try to pass a class by reference to a custom control class

How can I fix this?

namespace PR
{
    public partial class Joystick : UserControl
    {
        ref Al alStage;
        public Joystick(ref Al al_stage)
        {
            InitializeComponent();
            alStage = al_stage;
        }
    }
}

The error is

'Joystick.alStage() must declare a body because it is not marked abstract, extern, or partial'

答案1

得分: 1

I believe you may be a bit confused about the ref keyword.

From the C# docs:

> The ref keyword indicates that a variable is a reference, or an alias for another object.

In C# classes are reference types, and so when they are used as a method parameter, the method receives the reference to the class. Functionally this means that you are passing around a single instance of the class. Any operation in the method will affect the original object.

In contrast, structs and basic data types are value types, and when used as method parameters, the method receives a copy of the data type. Any operation in the method will not affect the original object.

ref changes this behavior. For value types, it means you're effectively passing it as a reference, and subsequent operations will affect the original copy.

EDIT: For reference types, it changes what the called method receives - by default the called method receives a copy of the address of the class - which means it can access and modify class members, but it cannot change the address for the calling class (it can't replace that instance with a new instance). When ref is used, the original address of the instance is passed to the called method, so it can replace the original instance with a new instance. (Thanks Orion for your correction)

To fix your error; simply remove the ref keywords.

More from the C# docs (C# Method Parameters)

> Because a struct is a value type, when you pass a struct by value to a method, the method receives and operates on a copy of the struct argument. The method has no access to the original struct in the calling method and therefore can't change it in any way. The method can change only the copy.

> A class instance is a reference type, not a value type. When a reference type is passed by value to a method, the method receives a copy of the reference to the class instance. That is, the called method receives a copy of the address of the instance, and the calling method retains the original address of the instance. The class instance in the calling method has an address, the parameter in the called method has a copy of the address, and both addresses refer to the same object. Because the parameter contains only a copy of the address, the called method cannot change the address of the class instance in the calling method. However, the called method can use the copy of the address to access the class members that both the original address and the copy of the address reference. If the called method changes a class member, the original class instance in the calling method also changes.

英文:

I believe you may be a bit confused about the ref keyword.

From the C# docs:

> The ref keyword indicates that a variable is a reference, or an alias for another object.

In C# classes are reference types, and so when they are used as a method parameter, the method receives the reference to the class. Functionally this means that you are passing around a single instnace of the class. Any operation in the method will affect the original object.

In contrast, structs and basic data types are value types, and when used as method parameters, the method receives a copy of the data type. Any operation in the method will not affect the original object.

ref changes this behaivour. For value types, it means you're effectively passing it as a reference, and subsequent operations will affect the original copy.

EDIT: For reference types, it changes what the called method receives - by default the called method receives a copy of the address of the class - which means it can access and modify class members, but it cannot change the address for the calling class (it can't replace that instance with a new instance. When ref is used, the original address of the instance is passed to the called method, so it can replace the original instance with a new instance. (Thanks Orion for your correction)

To fix your error; simply remove the ref keywords.

More from the C# docs (C# Method Parameters)

> Because a struct is a value type, when you pass a struct by value to a method, the method receives and operates on a copy of the struct argument. The method has no access to the original struct in the calling method and therefore can't change it in any way. The method can change only the copy.

>A class instance is a reference type, not a value type. When a reference type is passed by value to a method, the method receives a copy of the reference to the class instance. That is, the called method receives a copy of the address of the instance, and the calling method retains the original address of the instance. The class instance in the calling method has an address, the parameter in the called method has a copy of the address, and both addresses refer to the same object. Because the parameter contains only a copy of the address, the called method cannot change the address of the class instance in the calling method. However, the called method can use the copy of the address to access the class members that both the original address and the copy of the address reference. If the called method changes a class member, the original class instance in the calling method also changes.

huangapple
  • 本文由 发表于 2023年5月18日 08:23:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定