null-propagation-operator

Null Conditional Operator, not behaving as expected on some machines

谁说胖子不能爱 提交于 2020-01-04 07:36:20
问题 I have the following code in a DelegatingHandler for a WebApi 2 project. protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var content = await request.Content?.ReadAsStringAsync(); _log.Information("Received {request} with content body: {content}.", request, content); // Run the rest of the request pipeline. var result = await base.SendAsync(request, cancellationToken); var responseContent = await result.Content?

Why can't I use the null propagation operator in lambda expressions?

…衆ロ難τιáo~ 提交于 2019-12-17 10:46:11
问题 I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code throws a compile error that we can't use null propagating operator in lambda. var cnt = humans.AsQueryable().Count(a => a.House?[0].Price == 5000); The error : Error CS8072 An expression tree lambda may not contain a null propagating operator. C# Could easily translate above code to the code to

Error when using the null-propagating / null-conditional operator

血红的双手。 提交于 2019-12-04 03:28:13
问题 I am running a .NET 4.5 project in VS 2013. Why is the following code in error? var w = Request.Properties["MS_HttpContext"] as System.Web.HttpContextWrapper; string IP = w?.Request.UserHostAddress; //fail to compile I found this code on this MSDN blog. 回答1: That is a new feature available in C# 6 and newer versions. It is called the null-conditional operator. In order to use C# 6 you should download Visual Studio 2015 or a newer version, since the extension for Visual Studio 2013 isn't

Expression Tree for o?.Value

老子叫甜甜 提交于 2019-12-02 05:33:42
问题 I'd like to generate this sentence using Expression trees: o?.Value o is an instance of whichever class. Is there some way? 回答1: Normally, if you want to know how to construct an expression tree for some expression, you let the C# compiler do it and inspect the result. But in this case, it won't work, because "An expression tree lambda may not contain a null propagating operator." But you don't actually need the null propagating operator, you just need something that behaves like one. You can

Null-propagation replacement for null check prior conditional statement

旧巷老猫 提交于 2019-12-02 01:27:50
After seeing a similar question , I was wondering if the following expression ... if (attribute != null && attribute.Description == input) ... would behave (almost) identical, to following null-propagation variant? if (attribute?.Description == input) So far, I could determine only following (somehow minor) differences: not possible in case input is of non-nullable type in case input would be itself null , behavior would be altered Am I missing something? or are there other differences in behavior ? EDIT: in the end, the only fail-safe alternative I've found for the first snippet, would be: if

Operator '?' cannot be applied to operand of type 'T'

与世无争的帅哥 提交于 2019-11-30 09:13:14
问题 Trying to make Feature generic and then suddenly compiler said Operator '?' cannot be applied to operand of type 'T' Here is the code public abstract class Feature<T> { public T Value { get { return GetValue?.Invoke(); } // here is error set { SetValue?.Invoke(value); } } public Func<T> GetValue { get; set; } public Action<T> SetValue { get; set; } } It is possible to use this code instead get { if (GetValue != null) return GetValue(); return default(T); } But I am wondering how to fix that

C# 6.0 Null Propagation Operator & Property Assignment

江枫思渺然 提交于 2019-11-28 11:53:11
This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 in that you cannot call property setters against an object that has been null propagated (though you can call property getters against an object that has been null propagated). As you will see from the generated IL (which I have reflected into C#) , there is nothing that should limit the ability to call property setters using null propagation. To start with, I have created a simple class, with both

Null propagation operator, out parameters and false compiler errors?

ε祈祈猫儿з 提交于 2019-11-28 09:16:14
Let's assume I have a class that has a property of type Dictionary<string,string> , that may be null. This compiles but the call to TryGetValue() could throw at a NullRef exception at runtime: MyClass c = ...; string val; if(c.PossiblyNullDictionary.TryGetValue("someKey", out val)) { Console.WriteLine(val); } So I'm adding a null-propagating operator to guard against nulls, but this doesn't compile: MyClass c = ...; string val; if( c.PossiblyNullDictionary ?. TryGetValue("someKey", out val) ?? false ) { Console.WriteLine(val); // use of unassigned local variable } Is there an actual use case

Why can't I use the null propagation operator in lambda expressions?

試著忘記壹切 提交于 2019-11-27 12:31:55
I often use null propagating operator in my code because it gives me more readable code, specially in long queries I don't have to null-check every single class that is used. The following code throws a compile error that we can't use null propagating operator in lambda. var cnt = humans.AsQueryable().Count(a => a.House?[0].Price == 5000); The error : Error CS8072 An expression tree lambda may not contain a null propagating operator. C# Could easily translate above code to the code to following code if really can't do anything else! var cnt = humans.AsQueryable().Count(a => a.House != null &&

C# 6.0 Null Propagation Operator & Property Assignment

别说谁变了你拦得住时间么 提交于 2019-11-27 06:34:24
问题 This question has been completely overhauled in the interest of being thorough in explanation. I have noticed what appears to be quite a poor limitation of the null propagation operator in C# 6.0 in that you cannot call property setters against an object that has been null propagated (though you can call property getters against an object that has been null propagated). As you will see from the generated IL (which I have reflected into C#) , there is nothing that should limit the ability to