Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
像c#中的指针吗?_C#_C++_Class_Pointers_Call By Value - Fatal编程技术网

像c#中的指针吗?

像c#中的指针吗?,c#,c++,class,pointers,call-by-value,C#,C++,Class,Pointers,Call By Value,我是c#(一般来说是&编码)新手,我找不到任何与之相当的指针。 当我搜索谷歌时,我得到了一些类似安全/不安全的东西,但这不是我需要的。 就像C++中的,如果你有一个指针,它指向某个值,指针的变化会导致原始变量的变化。 在c#中有这样的东西吗? 范例- 编辑- 谢谢你@Sweeper我知道答案了 我必须使用ref int temp=ref universal.a C#具有与指针非常相似的引用。如果a和b都是对同一对象的引用,则a中的更改也将出现在b中 例如,在: class X { pub

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

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

编辑- 谢谢你@Sweeper我知道答案了 我必须使用ref int temp=ref universal.a

C#具有与指针非常相似的
引用。如果
a
b
都是对同一对象的引用,则
a
中的更改也将出现在
b

例如,在:

class X {
    public int val;
}

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


如果您将
X
的声明从
class
更改为
struct
,那么
a
b
将不再是引用,并且将写入
0

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

包装器对象 您可以创建这样一个类,它包装了一个
int

public class IntWrapper {
    public int Value { get; set; }
}
然后将
a
的类型更改为此类:

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
均指同一对象

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

public void change2()
{
    ref int temp = ref universal.a; //change in temp gives change in a
    temp -= 5;
}
在某些情况下(当非常需要优化时),您几乎可以使用类似C的指针。只有将代码置于
不安全的范围内,才能明确指定您知道风险:

不安全
{
整数=777;
int*ptr=&number;
WriteLine($“指针指向的数据是:{number}”);
WriteLine($“指针持有的地址:{(int)ptr}”);
}

不安全上下文允许您直接使用指针。请注意,默认情况下,此选项将从项目中关闭。要测试这一点,您需要右键单击project>Properties>Build-Allow unsafe code

在c#
中通过引用传递
而不是指针,下面是更正的代码

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();
}
像这样

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;
        }
    }
}

<> >编辑:我注意到这与清扫者的答案的最后一部分相同,但是我将把它放在这里,因为它只集中在那个解决方案上。[/P>和C++一样,如果你有一个指针,它指向某个值,指针的变化会导致原始变量的变化。在c#中有这样的东西吗再看看<代码>通过引用传递将解决您的问题。实际上,我的全部观点是,每次都必须写入“universal.a”,而只需写入“temp”(单变量)并在函数“change2”中使用该“temp”有没有什么方法可以让我们不必在函数参数中使用ref,而是直接在函数中指定temp?我的全部目的是不必每次都写环球a?只需通过函数change2()将其替换为temp,但temp中的更改仍然会导致universal中的更改。a您将在这里找到答案:使用
通过引用传递
,您已经声明该成员为静态成员,为什么不直接使用它?实际上我不想编写“universal.a”每次执行函数change2()时,只需将其替换为一个变量名temp,而必须写入temp.a实际上无法达到此目的_(ツ)_/“@WiredDifferently Oops。我复制粘贴了您的代码,并意外地将
.a
放在那里。如果使用
ref
局部变量,则不需要
.a
。如果使用包装对象,则需要
.a
值,但不需要
.a
。它仍然给出错误提示-“无法使用值初始化按引用变量“@wireddifferentily别忘了
ref
之前的
universal.a
这个词。我忘了,现在我把它加进去了。非常感谢,我得到了解决方案,但纯粹出于好奇,这个ref可以用于所有数据类型吗?就像我正在编写一些selenium代码,我需要使用IWebDriver来代替int一样(如果你不想回答,就不必回答这个问题,因为它与我发布的问题并不完全相关)
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;
        }
    }
}