C#中的ref问题 [英] Problem with ref in C#

查看:113
本文介绍了C#中的ref问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时,我得到

最好的重载匹配....有一些无效的参数。





使用System; 

namespace AccessModifiers
{
public class ClassA
{
public int x = 100;

public void Method1(ref int y)
{
Console.WriteLine(Method 1);
}

public void Method2()
{
Method1(ref x);
}
}
公共课程
{

public static void Main(string [] args)
{

ClassA c = new ClassA();

c.Method1(ref 5);


}
}
}





什么我试过了:



************************** *************************** ----------------------- -------------------------------------------------- --------------- *********************************

解决方案

看起来你没有仔细阅读教程。

其次你分享了错误的信息或错误的代码。从您分享的代码中,您应该收到类似的消息 -

Quote:

ref或out参数必须是一个可赋值变量



这意味着你不能将常量值作为ref参数传递。



所以,而不是

 c.Method1( ref   5  ); 



尝试关注 -

  int  myInt =  5 ; 
c.Method1( ref myInt);





Hope ,它会有所帮助。

如果我在这里错过了什么,请告诉我。

:)


when i run this code , i get
the best overloaded match for .... has some invalid arguments.


using System;

namespace AccessModifiers
{
    public class ClassA
    {
        public int x = 100;

        public void Method1(ref int y)
        {
            Console.WriteLine("Method 1 ");
        }

       public  void Method2()
        {
            Method1(ref x);
        }
    }
    public class Program
    {

        public static void Main(string[] args)
        {

            ClassA c = new ClassA();

            c.Method1(ref 5);


        }
    }
}



What I have tried:

*****************************************************----------------------------------------------------------------------------------------*********************************

解决方案

Looks like you haven't followed the tutorial closely.
Secondly you have shared the wrong message or wrong piece of code. From the code you have shared you should get a message something like -

Quote:

A ref or out argument must be an assignable variable


Which means you can not pass a constant value as a ref argument.

So, instead of

c.Method1(ref 5);


try following-

int myInt = 5;
c.Method1(ref myInt);



Hope, it helps.
Please let me know if I am missing something here.
:)


这篇关于C#中的ref问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆