C# 重新设计允许可变性的扩展方法(针对引用类型)
我一直使用不可变的扩展方法,并为它们执行的任何对象生成新的和改进的版本C# 重新设计允许可变性的扩展方法(针对引用类型),c#,extension-methods,immutability,value-type,C#,Extension Methods,Immutability,Value Type,我一直使用不可变的扩展方法,并为它们执行的任何对象生成新的和改进的版本 public static ReferenceType Biggify(this ReferenceType self) { return self.Something(); } 现在我意识到,让这样一个扩展方法对self做一些事情,而不是像这样返回一个jack可能会很好 public static void Biggify(this ReferenceType self) { self = self.Someth
public static ReferenceType Biggify(this ReferenceType self)
{
return self.Something();
}
现在我意识到,让这样一个扩展方法对self做一些事情,而不是像这样返回一个jack可能会很好
public static void Biggify(this ReferenceType self)
{
self = self.SomethingElse();
}
然而,我意识到上述操作将只在self的一个副本上执行,并且当我们退出该方法的范围时,突变将被丢弃
this
)参数不能是ref
,因此不能修改传入对象的引用
事实上,如果对象本身是可变扩展,则可以轻松地显式更改对象:
void AddCouple<T>(this List<T> list) where T:new()
{
list.Add(new T());
list.Add(new T());
}
void AddCouple(此列表),其中T:new()
{
添加(新的T());
添加(新的T());
}
或无意中喜欢:
List<T> AddToCopy<T>(this List<T> list, T newItem)
{
var newList = list;
newList.Add(newItem); // changed existing list...
return newList;
}
List AddToCopy(此列表,T newItem)
{
var newList=list;
newList.Add(newItem);//已更改现有列表。。。
返回newList;
}
中还有一些额外的讨论,特别是关于不可变类型/值类型的扩展。1。这取决于
此
参数是值类型还是引用类型。字符串始终是不可变的,因此不能对字符串执行此操作。如果它是引用类型,尽管它将按预期工作。@Jashaszun将其作为回复,而不是注释。另外,请注意,甚至还有第3个问题。。。这是我最感兴趣的部分。(至于类型,我忘记了区别。我将更新这个问题。)这里的扩展方法没有什么特别的地方,只是第一个参数不能是ref
参数。如果希望它是可变的,为什么不只是变异self
而不是执行赋值呢?