lifted-operators

System.Nullable<T> What is the value of null * int value?

冷暖自知 提交于 2020-01-14 12:41:32
问题 Consider the following statements: int? v1 = null; int? v2 = 5 * v1; What is the value of v2 ? ( null or empty string?) How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling? 回答1: It's null . C# Language Specification 3.0 (Section §7.2.7: Lifted operators) For the binary operators + - * / % & | ^ << >> : a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted form is constructed by

System.Nullable<T> What is the value of null * int value?

送分小仙女□ 提交于 2020-01-14 12:40:58
问题 Consider the following statements: int? v1 = null; int? v2 = 5 * v1; What is the value of v2 ? ( null or empty string?) How can I prevent the compiler to mark it as invalid operation? Do I need to follow custom exception handling? 回答1: It's null . C# Language Specification 3.0 (Section §7.2.7: Lifted operators) For the binary operators + - * / % & | ^ << >> : a lifted form of an operator exists if the operand and result types are all non-nullable value types. The lifted form is constructed by

Wrong compiler warning when comparing struct to null

喜你入骨 提交于 2019-12-18 19:27:29
问题 Consider the following code: DateTime t = DateTime.Today; bool isGreater = t > null; With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning: warning CS0458: The result of the expression is always 'null' of type 'bool?' This is incorrect; the result is always false (of type bool ): Now, the struct DateTime overloads the > (greater than) operator. Any non-nullable struct (like DateTime) is implicitly convertible to the corresponding Nullable<> type. The above expression is

Wrong compiler warning when comparing struct to null

谁说胖子不能爱 提交于 2019-11-30 18:12:24
Consider the following code: DateTime t = DateTime.Today; bool isGreater = t > null; With Visual Studio 2010 (C# 4, .NET 4.0), I get the following warning: warning CS0458: The result of the expression is always 'null' of type 'bool?' This is incorrect; the result is always false (of type bool ): Now, the struct DateTime overloads the > (greater than) operator. Any non-nullable struct (like DateTime) is implicitly convertible to the corresponding Nullable<> type. The above expression is exactly equivalent to bool isGreater = (DateTime?)t > (DateTime?)null; which also generates the same wrong

Why are there no lifted short-circuiting operators on `bool?`?

淺唱寂寞╮ 提交于 2019-11-27 20:09:53
Why doesn't bool? support lifted && and || ? They could have lifted the true and false operators which would have indirectly added lifted && and || . The operators | and & are already lifted and implement the correct Three-valued logic . But of course they are not short circuiting like || and && . The question is why they decided not to lift those operators when creating the specification. So "It's like this because the spec says so" is no answer to the "why?". When lifting true and false so that null is neither true nor false : public static bool operator true(bool? x) { return x.HasValue &&

Why are there no lifted short-circuiting operators on `bool?`?

十年热恋 提交于 2019-11-26 20:13:17
问题 Why doesn't bool? support lifted && and || ? They could have lifted the true and false operators which would have indirectly added lifted && and || . The operators | and & are already lifted and implement the correct Three-valued logic. But of course they are not short circuiting like || and && . The question is why they decided not to lift those operators when creating the specification. So "It's like this because the spec says so" is no answer to the "why?". When lifting true and false so

What are lifted operators?

折月煮酒 提交于 2019-11-26 17:23:36
I was looking at this article and am struggling to follow the VB.NET example that explains lifted operators. There doesn't seem to be an equivalent C# example or tutorial. I don't have much experience with operator overloading in general, so trying to come to terms with the VB.NET equivalent whilst reading up on nullable types probably isn't the best place to start... Would anyone be able to provide an explanation of lifted operators and how they are used by nullable types? Does it just mean that the nullable type does not itself overload operators and will use the operators from the

What are lifted operators?

和自甴很熟 提交于 2019-11-26 05:25:29
问题 I was looking at this article and am struggling to follow the VB.NET example that explains lifted operators. There doesn\'t seem to be an equivalent C# example or tutorial. I don\'t have much experience with operator overloading in general, so trying to come to terms with the VB.NET equivalent whilst reading up on nullable types probably isn\'t the best place to start... Would anyone be able to provide an explanation of lifted operators and how they are used by nullable types? Does it just