-2

我是 c# 的新手(一般是和编码),我找不到任何等效的指针。
当我搜索谷歌时,我得到了安全/不安全之类的东西,但这不是我需要的。

就像在 c++ 中一样,如果你有一个指针并且它指向某个值,那么指针的变化会导致原始变量的变化。c#中有这样的东西吗?
例子-

static class universal
{
   public static int a = 10;
}

class class_a
{
   public void change1()
   {
      universal.a--;
   }
}

class class_b
{
   public void change2()
   {
      some_keyword temp = universal.a; //change in temp gives change in a
      temp-= 5; //the purpose of temp is to NOT have to write universal.a each time
   }
}

...

static void Main(string[] args)
{
   class_b B = new class_b();
   class_a A = new class_a();
   A.change1();
   Console.WriteLine(universal.a);//it will print 9

   B.change2();
   Console.WriteLine(universal.a);//it will print 4
   Console.ReadKey();
}

编辑-谢谢@Sweeper,我得到了答案,我必须使用 ref int temp = ref universal.a;

4

5 回答 5

2

C#references与指针非常相似。如果ab都是对同一个对象的引用,那么在 中a也会出现变化b

例如,在:

class X {
    public int val;
}

void Main()
{
    var a = new X();
    var b = a;
    a.val = 6;
    Console.WriteLine(b.val);
}

6将被写入。

如果您将Xfrom的声明更改classstruct,则aandb将不再是引用,并且0将被写入。

于 2020-12-04T08:53:01.273 回答
2

如果您不想要不安全的代码,我可以想到两个选项。

包装对象

你可以创建一个这样的类,它包含一个int

public class IntWrapper {
    public int Value { get; set; }
}

然后将a's 的类型更改为此类:

static class Universal
{
   public static IntWrapper a = new IntWrapper { Value = 10 };
}

class class_a
{
   public void change1()
   {
      universal.a.Value--;
   }
}

class class_b
{
   public void change2()
   {
      Universal temp = universal.a; //change in temp gives change in a
      temp.Value -= 5;
   }
}

这是因为类是引用类型,并且a持有对对象的引用(类似于指针)IntWrapper=复制对 的引用temp,而不创建新对象。temp和都a指同一个对象。

ref当地人

这是一种更简单的方法,但仅适用于局部变量。例如,您不能将其用于字段。

public void change2()
{
    ref int temp = ref universal.a; //change in temp gives change in a
    temp -= 5;
}
于 2020-12-04T09:01:29.427 回答
2

在某些情况下(当非常需要优化时),您可以使用几乎类似于 C 的指针。您只能通过将代码置于unsafe范围内明确指定您意识到风险来做到这一点:

unsafe
{
    int number = 777;
    int* ptr = &number;

    Console.WriteLine($"Data that pointer points to is: {number} ");
    Console.WriteLine($"Address that pointer holds: {(int)ptr}");
}

不安全的上下文允许您直接使用指针。请注意,默认情况下,您的项目会关闭此选项。要对此进行测试,您需要右键单击项目>属性>构建 - 允许不安全代码

于 2020-12-04T09:02:58.730 回答
2

在 c#Pass By Reference中使用而不是指针,这是更正的代码

static class universal
{
   public static int a = 10;
}

class class_a
{
   public void change1()
   {
      universal.a--;
   }
}

class class_b
{
   public void change2(ref int val)//use ref keyword for reference
   {
       int temp = val; //change in temp gives change in a
       val -= 5;
   }
}

static void Main(string[] args)
{
   class_b B = new class_b();
   class_a A = new class_a();
   A.change1();
   Console.WriteLine(universal.a);//it will print 9

   B.change2(ref universal.a); //pass value by reference using ref keyword
   Console.WriteLine(universal.a);//it will print 4
   Console.ReadKey();
}
于 2020-12-04T09:16:03.240 回答
0

像这样?

using System;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            var test = new class_a();
            test.change1();
            Console.WriteLine(universal.a); // Prints 18
        }
    }

    static class universal
    {
        public static int a = 10;
    }

    class class_a
    {
        public void change1()
        {
            ref int x = ref universal.a;

            ++x;
            ++x;
            ++x;
            x += 5;
        }
    }
}

[编辑:我注意到这与 Sweeper 答案的最后一部分相同,但我将把它留在这里,因为它只关注那个解决方案。]

于 2020-12-04T09:38:01.850 回答