开发者

Overwrite binary operators in C#

开发者 https://www.devze.com 2022-12-13 22:28 出处:网络
Hello I have a Expression.GreaterThanOrEqual(left, right), so I want to evaluate string or d开发者_运维问答omain types with data like :

Hello I have a Expression.GreaterThanOrEqual(left, right), so I want to evaluate string or d开发者_运维问答omain types with data like :

002-1400

so when I use GreaterThanOrEqual operator ">=", I will be able to say 002-1400 "is greater or equal than" 002-1400, 002-1399, 002-1398 ... So I can play with some grid rows for compare via input parameters.

Hopes be understandable,

Thanks,


You cannot define operators on the String class. You can however create your own type and define equality relationships in any way you chose.


You would override it something like this:

public static operator >=(Foo foo1, Foo foo2)
{
    //code here...
}
public static operator ==(Foo foo1, Foo foo2)
{
    return foo1.Equals(foo2);
}
//etc... don't forget to implement Equals and GetHashCode!

see this question for hashcode implementing ideas.

Edit: Ed Swangren is right, you can't override an operator on a builtin class. You can, however, override it on your own classes!


Look for operator overloading. See MSDN: Operator Overloading Tutorial for example.


You can to use operator overloading. See this link for a tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号