Tag: operator overloading

为什么不能在C#中共存相同类型的隐式和显式运算符?

为什么不能在同一个类中共存两个相同类型的运算符(显式和隐式)? 假设我有以下内容: public class Fahrenheit { public float Degrees { get; set; } public Fahrenheit(float degrees) { Degrees = degrees; } public static explicit operator Celsius(Fahrenheit f) { return new Celsius(ToCelsius(f.Degrees)); } public static implicit operator Celsius(Fahrenheit f) { return new Celsius(ToCelsius(f.Degrees)); } } public class Celsius { public float Degrees { get; set; } […]

为什么在重载相等运算符时期望覆盖GetHashCode和Equals?

重载Equals运算符时未能覆盖GetHashCode和Equals会导致编译器产生警告。 为什么改变其中任何一个的实现是个好主意? 在阅读了Eric Lippert关于GetHashCode的博客文章之后,似乎可能没有很多有用的替代GetHashCode的基础实现,为什么编译器我鼓励你改变它?

在通用c#类中链接隐式运算符

对于以下通用c#类,我想将T转换为K: public abstract class ValueType : IValueType where K : ValueType,new() { public abstract T Value { get; set; } public static implicit operator ValueType(T val) { K k = new K(); k.Value = val; return k; } } 如果我要实现直接运算符implicit operator K(T val) ,则会导致编译时错误(CS0556)。 我以为我可以尝试链接隐式运算符: public static implicit operator K(ValueType vt){ return (K)val; } 但是下面的例子仍然抱怨它无法转换: […]

OR(|),XOR(^),AND(&)重载

如何重载&,| C#中的^和^运算符以及重载如何工作? 我一直试图找到一些好的答案,但没有找到任何答案。 我将分享任何有用的文章。

C#中的隐式数组转换

我有以下类定义了隐式转换运算符: class A { … } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } 现在,我可以隐含地将A转换为B. 但为什么我不能隐含地将A []强制转换为B []? static void Main(string[] args) { // compiles A a = new A(); B b = a; // doesn’t compile […]

C#+运算符调用string.concat函数?

可能重复: C#是否优化了字符串文字的串联? 我刚刚发现我们写了这样一行: string s = “string”; s = s + s; // this translates to s = string.concat(“string”, “string”); 但是我通过reflection器打开了字符串类,我没看到这个+运算符在哪里重载? 我可以看到==和!=超载。 [TargetedPatchingOptOut(“Performance critical to inline across NGen image boundaries”)] public static bool operator ==(string a, string b) { return string.Equals(a, b); } [TargetedPatchingOptOut(“Performance critical to inline across NGen image boundaries”)] public static bool operator […]

使用变量generics委托类型对operator ==进行重载解析

在两个委托类型表达式之间使用==进行重载解析的精确规则是什么? 请考虑以下代码(需要using System; ): static class ProgramA { static void TargetMethod(object obj) { } static void Main() { Action instance1 = TargetMethod; Action instance2 = TargetMethod; Action a1 = instance1; Action a2 = instance2; Console.WriteLine((object)a1 == (object)a2); Console.WriteLine((Delegate)a1 == (Delegate)a2); Console.WriteLine((Action)a1 == (Action)a2); Console.WriteLine(a1 == a2); // warning CS0253: Possible unintended reference comparison; to get […]

Linq和Equality Operator:类型’System.Int32’的表达式不能用于’System.Object’类型的参数

我正在尝试覆盖C#中的相等(==)运算符来处理将任何类型与自定义类型进行比较(自定义类型实际上是一个围绕null的包装器/框)。 所以我有这个: internal sealed class Nothing { public override bool Equals(object obj) { if (obj == null || obj is Nothing) return true; else return false; } public static bool operator ==(object x, Nothing y) { if ((x == null || x is Nothing) && (y == null || y is Nothing)) return true; return […]

我应该重载==运营商吗?

==运算符如何在C#中真正起作用? 如果它用于比较A类的对象,它会尝试匹配所有A的属性,还是会寻找指向同一内存位置的指针(或者其他东西)? 让我们创建一个假设的例子。 我正在编写一个使用Twitter API的应用程序,它有一个Tweet类,它具有单个推文的所有属性:文本,发送者,日期和时间,源等。如果我想比较类Tweet的对象是否等价,我可以使用: Tweet a, b; if (a == b) { //do something… } 那会检查 a和b之间Tweet类的所有属性的等价吗? 如果没有, 那么正确的方法是重载==运算符以明确检查所有字段的等价性吗? 更新:从前两个答案,我是正确的假设: 如果没有为类重载==运算符或Equals方法,则使用对象类的==运算符。 对象类的==运算符检查内存位置是否相等。 我必须重载==运算符或Equals方法来完成此任务。 在重载中,我必须手动检查属性的等价性,所以没有办法半自动地,例如,在循环中 ,对吧? 更新#2: Yuriy发表评论说,可以使用reflection检查==运算符中属性的等价性。 如何才能做到这一点? 你能给我一些示例代码吗? 谢谢!

为什么C#编译器不调用隐式转换运算符?

假设我们有以下类型: struct MyNullable where T : struct { T Value; public bool HasValue; public MyNullable(T value) { this.Value = value; this.HasValue = true; } public static implicit operator T(MyNullable value) { return value.HasValue ? value.Value : default(T); } } 并尝试编译以下代码片段: MyNullable i1 = new MyNullable(1); MyNullable i2 = new MyNullable(2); int i = i1 + […]