C# 引用赋值运算符?

发布于 2024-12-10 20:25:25 字数 189 浏览 0 评论 0原文

例如:

        int x = 1;
        int y = x;
        y = 3;
        Debug.WriteLine(x.ToString());

是否是任何引用运算符而不是 line:3 上的“=”, 如果我指定 y =3 ,则使 x 等于 3 。

for example:

        int x = 1;
        int y = x;
        y = 3;
        Debug.WriteLine(x.ToString());

Is it any reference operator instead of "=" on line:3,
to make the x equal to 3 , if i assign y =3 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一身软味 2024-12-17 20:25:25

我曾经写过一个具有该功能的 C# 版本的原型;你可以说:

int x = 123;
ref int y = ref x;

现在 x 和 y 将是同一变量的别名。

我们决定不将该功能添加到该语言中;如果您有一个非常棒的用例,我很想听听。

您不是第一个询问此功能的人;请参阅我可以使用参考吗在 C# 函数(如 C++)中? 了解更多详细信息。

更新:该功能可能会出现在 C# 7 中。

I once wrote a prototype of a version of C# that had that feature; you could say:

int x = 123;
ref int y = ref x;

and now x and y would be aliases for the same variable.

We decided to not add the feature to the language; if you have a really awesome usage case I'd love to hear it.

You're not the first person to ask about this feature; see Can I use a reference inside a C# function like C++? for more details.

UPDATE: The feature will likely be in C# 7.

奢欲 2024-12-17 20:25:25

正如您所发现的,“int”是一种值类型,因此会在赋值时复制值。

您可以通过使用“不安全”块来使用传统 C/C++ 意义上的指针,但指针只能在该块内使用,这限制了其使用。相反,如果您想访问“不安全”块之外的值,那么您需要转换为使用引用。

像这样的东西...

var x = new Tuple<int>(1);
var y = x;
y.Item1 = 3;

'int' is a value type and so copies the value on assignment, as you have discovered.

You could use pointers in the traditional C/C++ sense by using an 'unsafe' block but then the pointers are only usable inside that block which limits its use. If instead you want to access the value outside of an 'unsafe' block then you need to convert to using a reference instead.

Something like this...

var x = new Tuple<int>(1);
var y = x;
y.Item1 = 3;
请爱~陌生人 2024-12-17 20:25:25

这不完全是您要问的,但我认为这可能会有所帮助。如果您想在函数内使用指针别名,可以使用 ref 关键字,如下所示:

public static void RefExample(ref int y)
{
    y = 3;
}

main()
{
    int x = 5;
    RefExample(ref x);
    Console.WriteLine(x); // prints 3
}

This is not exactly what you're asking, but I thought it might be helpful. If you want a pointer alias inside a function, you can use the ref keyword like such:

public static void RefExample(ref int y)
{
    y = 3;
}

main()
{
    int x = 5;
    RefExample(ref x);
    Console.WriteLine(x); // prints 3
}
北恋 2024-12-17 20:25:25

您可以像在C中一​​样使用指针

    int x = 1;
    unsafe
    {
        int* y = &x;
        *y = 3;
    }

    Debug.WriteLine(x.ToString());

(注意您必须使用不安全标志进行编译)

You can use pointers like in C

    int x = 1;
    unsafe
    {
        int* y = &x;
        *y = 3;
    }

    Debug.WriteLine(x.ToString());

(Note you have to compile with the unsafe flag)

滥情哥ㄟ 2024-12-17 20:25:25

做到这一点的唯一方法是使用“不安全”代码,并实际使用指针。在 C# 中,指针不能存在于不安全代码块之外。然后,您应该能够像在 C/C++ 中一样使用指针

查看 页面了解如何使用“不安全”代码块。

The only way to do this is to use "unsafe" code, and actually use pointers. Pointers cannot exist outside of unsafe code blocks in C#. You should then be able to use pointers the same way you do in C/C++

Check out this page for how to use "unsafe" code blocks.

无远思近则忧 2024-12-17 20:25:25

我想评论 Eric Lippert 的答案,但新用户无法评论帖子。所以,我想我有这样的用法。

看看这段代码:

private void SetGridColumns(ref RegistryKey targetKey, List<ColInfo> cols)
    {
        string targetKeyName = Path.GetFileName(targetKey.Name);
        m_grids.DeleteSubKeyTree(targetKeyName, false);
        targetKey.Close();
        targetKey = m_grids.CreateSubKey(targetKeyName);

//...

    public void SetColumns(List<ColInfo> cols, bool youth)
    {
        RegistryKey key = youth ? m_youthGrid : m_mainGrid;
        SetGridColumns(ref key, cols);
    }

应该像这样工作:
在 SetColumns 中,我根据“youth”参数调用 SetGridColumns 。我希望先删除我的密钥,然后重新创建。 m_mainGrid 当然是类的成员。
在这种情况下,密钥确实被删除并重新创建。但重新创建的只是SetGridColumns中的“targetKey”,而不是我的m_mainGrid。

因此,我在这里唯一能做的就是使用指针,这在 C# 中不是首选方式。
如果我能做到的话:

ref RegistryKey key = youth ? m_youthGrid : m_mainGrid;

一切都应该正常进行。

I wanted to comment Eric Lippert's answer, but new users cannot comments posts. So, I think I have such usage.

Take a look at this code:

private void SetGridColumns(ref RegistryKey targetKey, List<ColInfo> cols)
    {
        string targetKeyName = Path.GetFileName(targetKey.Name);
        m_grids.DeleteSubKeyTree(targetKeyName, false);
        targetKey.Close();
        targetKey = m_grids.CreateSubKey(targetKeyName);

//...
}

    public void SetColumns(List<ColInfo> cols, bool youth)
    {
        RegistryKey key = youth ? m_youthGrid : m_mainGrid;
        SetGridColumns(ref key, cols);
    }

It should work like that:
In SetColumns I call SetGridColumns with key depending on "youth" param. I would like my key to be first deleted and then recreated. m_mainGrid is of course member of a class.
In this case, key is indeed deleted and recreated. But recreated is only "targetKey" in SetGridColumns, not my m_mainGrid.

So, the only thing I can do here is to make usage of pointers which is not preferred way in C#.
If I could only do:

ref RegistryKey key = youth ? m_youthGrid : m_mainGrid;

everything should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文