Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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# 为什么INotifyPropertyChanged的SetProperty()方法需要一个ref参数?_C#_Inotifypropertychanged_Ref - Fatal编程技术网

C# 为什么INotifyPropertyChanged的SetProperty()方法需要一个ref参数?

C# 为什么INotifyPropertyChanged的SetProperty()方法需要一个ref参数?,c#,inotifypropertychanged,ref,C#,Inotifypropertychanged,Ref,考虑到INotifyPropertyChanged的实现,通常如下所示: 可观察的公共类:INotifyPropertyChanged { 公共事件属性更改事件处理程序属性更改; 受保护的无效SetPropertyref T存储,T值,[CallerMemberName]字符串propertyName=null { 如果等于存储,则为值 { 回来 } 储存=价值; OnPropertyChangedpropertyName; } 受保护的无效OnPropertyChangedstring pro

考虑到INotifyPropertyChanged的实现,通常如下所示:

可观察的公共类:INotifyPropertyChanged { 公共事件属性更改事件处理程序属性更改; 受保护的无效SetPropertyref T存储,T值,[CallerMemberName]字符串propertyName=null { 如果等于存储,则为值 { 回来 } 储存=价值; OnPropertyChangedpropertyName; } 受保护的无效OnPropertyChangedstring propertyName=>PropertyChanged?.Invokethis,新的PropertyChangedEventArgspropertyName; } 为什么SetPropertymethod需要ref参数?除了类字段之外,几乎不可能将任何内容传递给该方法,因此它应该始终是引用类型


注意:我问这个问题是因为我想对通过foreach循环枚举的项使用此方法,该循环不适用于ref关键字。

目的是通过引用传递字段以及新值

如果这不是ref参数,您将传递字段的值。。。在这一点上,该声明:

storage = value;
。。。这是毫无意义的。这将更改参数的值,但根本不会修改字段

下面是一个完整的示例来演示这种差异:

using System;

class Program
{
    static string field;

    static void Main()
    {
        field = "initial value";
        Console.WriteLine($"Before Modify1: {field}");

        Modify1(field, "new value for Modify1");
        Console.WriteLine($"After Modify1: {field}");

        Modify2(ref field, "new value for Modify2");
        Console.WriteLine($"After Modify2: {field}");
    }

    static void Modify1(string storage, string value)
    {
        // This only changes the parameter
        storage = value; 
    }

    static void Modify2(ref string storage, string value)
    {
        // This changes the variable that's been passed by reference,
        // e.g. a field
        storage = value;
    }        
}
输出:

Before Modify1: initial value
After Modify1: initial value
After Modify2: new value for Modify2

如您所见,不带ref的Modify1根本不会修改字段,而Modify2会修改字段。

目的是通过引用传递字段以及新值

如果这不是ref参数,您将传递字段的值。。。在这一点上,该声明:

storage = value;
。。。这是毫无意义的。这将更改参数的值,但根本不会修改字段

下面是一个完整的示例来演示这种差异:

using System;

class Program
{
    static string field;

    static void Main()
    {
        field = "initial value";
        Console.WriteLine($"Before Modify1: {field}");

        Modify1(field, "new value for Modify1");
        Console.WriteLine($"After Modify1: {field}");

        Modify2(ref field, "new value for Modify2");
        Console.WriteLine($"After Modify2: {field}");
    }

    static void Modify1(string storage, string value)
    {
        // This only changes the parameter
        storage = value; 
    }

    static void Modify2(ref string storage, string value)
    {
        // This changes the variable that's been passed by reference,
        // e.g. a field
        storage = value;
    }        
}
输出:

Before Modify1: initial value
After Modify1: initial value
After Modify2: new value for Modify2

如您所见,不带ref的Modify1根本没有修改字段,而Modify2修改了字段。

阅读文档中的关键字。它将准确地告诉您它的功能…谢谢,虽然我知道ref关键字的功能,但我的问题是为什么在这种情况下需要它。如果您知道ref关键字的功能,那么您也知道SetProperty的功能,即SetProperty为什么使用ref关键字。你为什么要问?o、 o?SetProperty对存储的唯一作用是设置存储=值。若存储并没有通过引用传递,那个么这行代码将什么也不做。If将在方法中局部更新该变量,但这并不重要,因为在该方法之后,该变量将不再使用。它将是一个引用,由值传递。这与通过引用传递的变量不同。有关更多详细信息,请参阅。请阅读有关关键字的文档。它将准确地告诉您它的功能…谢谢,虽然我知道ref关键字的功能,但我的问题是为什么在这种情况下需要它。如果您知道ref关键字的功能,那么您也知道SetProperty的功能,即SetProperty为什么使用ref关键字。你为什么要问?o、 o?SetProperty对存储的唯一作用是设置存储=值。若存储并没有通过引用传递,那个么这行代码将什么也不做。If将在方法中局部更新该变量,但这并不重要,因为在该方法之后,该变量将不再使用。它将是一个引用,由值传递。这与通过引用传递的变量不同。有关更多详细信息,请参阅。感谢您的回答和示例!谢谢你的回答和例子!