Skip to main content
Filter by
Sorted by
Tagged with
1 vote
2 answers
128 views

Param( [String] $foo ) $foo ??= "bar" # does not work $foo = $foo ?? "bar" # does not work $foo = $foo ? $foo : "bar" # works write-host $foo I am using Powershell ...
GrandeKnight's user avatar
0 votes
0 answers
32 views

I try to add two values of nullable items. I noticed that expression returns 2 instead of expected 7, but I would like to understand why. Code: public class ListNode { public int val; public ...
Catherine's user avatar
0 votes
0 answers
28 views

In my opinion, the fewer conditionals and checks I need to do, the better. When I use C# a common pattern for me is declaring events something like this: public event Action MyEvent = delegate { }; ...
OmniOwl's user avatar
  • 5,729
0 votes
1 answer
56 views

So I have this code: foreach (var user in ServersHolder.Servers.Find(f => f.ID == 9999).Online.FindAll(g => g.LoginPhase == 3)) { await trySendPacket(9999, user.GUID.ToString(), OpCode....
Josh's user avatar
  • 47
1 vote
1 answer
75 views

I didn't know what better title to chose for the question as I don't know what's the problem. Consider the following small C# code: static void Main(string[] args) { F1(null); } public static void ...
Gigi's user avatar
  • 330
3 votes
1 answer
153 views

Why after using null-conditional-operator in nullable context (#nullable enable ) the static analyzer shows CS8602 warning? var test = new List<int>(); Console.WriteLine(test.Count); // Ok ...
Sinatr's user avatar
  • 22.3k
1 vote
0 answers
68 views

I would like to add MapWinGis to my C# application ,but I don't know the following: Whats the difference between (examples 1 and 2) and the meaning of the [question mark] in the first sample? ...
Steve's user avatar
  • 13
1 vote
1 answer
124 views

I am wondering about the behavioral difference that can be seen in output for Test 2 on example below. I'm on .NET Core 3.1 for this scenario. using System; public class Program { ...
furkan.kaya's user avatar
0 votes
2 answers
231 views

using System; public class A{ public bool func(){ return true; } public int func2(){ return 10; } } public class HelloWorld { public static void Main(string[...
신동철's user avatar
0 votes
1 answer
690 views

I am wondering whether there is a difference in performance between these two if-statements: if(myObject != null && myObject.someBoolean) { // do something } if (myObject?.someBoolean ?? ...
Florian Wolf's user avatar
1 vote
1 answer
90 views

Let's imagine the following situation: public class A { private readonly Func<bool> _myFunction; ... public A(Func<bool> myFunction) { _myFunction = myFunction ?? ...
Lenakeiz's user avatar
1 vote
3 answers
122 views

Are function arguments always evaluated in a C# null-conditional function call? i.e. in the following code: obj?.foo(bar()); Is bar evaluated if obj is null?
Nathan Phillips's user avatar
1 vote
1 answer
468 views

Get-CimInstance -ComputerName $pc -Class Win32_UserProfile | Where-Object { $_.LocalPath.split('\')[-1] -eq $compare } | Remove-CimInstance Because some system profiles have empty values for ...
user1245735's user avatar
2 votes
1 answer
323 views

if (value2 != null) { value1 = value2; } ? and ?? operators don't seem to be useful here. I thought of using the ternary operator. value1 = (value2 != null) ? value2 : value1; ...
esege's user avatar
  • 129
0 votes
2 answers
480 views

I'm trying to do the following check: if (result?.Pets?.Any() == false) { ... } and if result == null .. then it's not going inside the scope of that if check. I thought that result?. will (in this ...
Pure.Krome's user avatar
  • 87.5k
1 vote
1 answer
369 views

I am trying to get value from the HttpContext with a key Correlation-Context, but I am not sure why I am getting the error while trying to use the variable json. internal static CorrelationContext ...
Rasik's user avatar
  • 2,529
4 votes
2 answers
1k views

I'm using Newtonsoft to deserialize an known JSON object and retrieve some values from it if they're present. The crux is there is that object structure may keep changing so I'm using dynamic to ...
rboy's user avatar
  • 2,195
0 votes
2 answers
413 views

I have an interface<T>. This interface has a method Compare(T x, T y). x can never be null. y has a large chance of being null. I want to make this clear by using the Null-conditional operator ...
Zenith's user avatar
  • 327
1 vote
2 answers
7k views

Am refactoring some old code and see something in the following format: Type = rec["IsFlagged"]?.ToString() == "True" ? "Yes" : "No" which should not work if ...
N. Kaufman's user avatar
0 votes
2 answers
191 views

I saw the below code from MSDN: using System; public sealed class Foo : IDisposable { private readonly IDisposable _bar; public Foo() { _bar = new Bar(); } public void ...
user avatar
4 votes
2 answers
829 views

Can someone please explain the logic of the null-conditional operator in if statements? Imagine the following code List<string> items = null; if (items?.Count == 0) { Console.WriteLine("...
David's user avatar
  • 830
0 votes
1 answer
477 views

PowerShell 7 recently moved out of the experimental stage the Null-conditional (?.) operator. In playing around with it a bit and reading the examples in the link above I think I understand how it ...
codewario's user avatar
  • 21.9k
1 vote
0 answers
426 views

This works, calling Invoke only if the member is not null: List<Command> list = GetCommands(); list.FirstOrDefault(predicate)?.Invoke(); Conditionally calling a property setter does not work: ...
Hand-E-Food's user avatar
  • 12.9k
0 votes
1 answer
541 views

I just noticed that property assignment is not allowed in combination with the null conditional operator. With methods is it not a problem at all though. I always thought that properties are just ...
Dirk Boer's user avatar
  • 9,251
0 votes
2 answers
452 views

const addendum = null const acts.change = [act1]; console.log( acts.change.concat(addendum?.changedActions) ); Outputs [act1, null] rather than the expected [act1]. Am I misusing the null-conditional ...
cjcurrie's user avatar
  • 622
1 vote
1 answer
130 views

env: VS2017 v15.9.24, .net framework 2.0 c# console project. this is a very simple console project, no any reference, all codes are in program.cs: namespace ConsoleApp1 { class Program { ...
ahdung's user avatar
  • 409
2 votes
1 answer
580 views

I wonder why Visual Studio 2019 doesn’t complain about this piece of C# code: dynamic deserializedBody = JsonConvert.DeserializeObject(requestBody); deserializedBody?.date.ToString(); Since ...
Pine Code's user avatar
  • 2,877
1 vote
1 answer
250 views

There's a modern idiom in C# that utilizes the null-conditional operator to promote thread-safety when using delegates. Namely: Update?.Invoke(this, theArgs); Why does this work? Does the C# ...
estefano-ppt's user avatar
0 votes
0 answers
497 views

I know this is asked in various situations, but I didn't find this exact question, or satisfying answers, sorry, if I wasn't enough cautious. Symptoms: When having nested objects, Like Class A ...
beatcoder's user avatar
  • 733
1 vote
3 answers
654 views

Why there's no Null-Conditional Operator for events ? For example i have following code which raise event if object is not null : Button TargetButton = null; if(IsRunning) { ...
deveton's user avatar
  • 361
12 votes
3 answers
6k views

C# and other languages have null-conditionals usually ?. A?.B?.Do($C); Will not error out when A or B are null. How do I achieve something similar in powershell, what's a nicer way to do: if ($A) { ...
RaGe's user avatar
  • 24k
0 votes
1 answer
368 views

I have following XML file that I am parsing using XML serializer. <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Projects> <Global> <Variables> ...
YogiWatcher's user avatar
-1 votes
2 answers
679 views

I have misunderstanding some of Null-conditional operator ?. mechanism Example getting Length of String variable object variable = null; int? Length = variable?.ToString()?.Trim()?.Length; Should we ...
c sharp's user avatar
  • 17
3 votes
3 answers
573 views

I have 2 statements that use the null conditional (?) operator and perform ToString on the result. These 2 statement seem like they should have the same result, but they do not. The only different is ...
Eric Smith's user avatar
2 votes
4 answers
632 views

Reference I'm currently dealing with some thread sensitive code. In my code I have a list of objects that is manipulated by two different threads. One thread can add objects to this list, while the ...
Loocid's user avatar
  • 6,511
4 votes
2 answers
346 views

I'm confused about how the null-conditional operator cascades with normal property access. Take these two examples: a?.b.c (a?.b).c I would expect them to be equivalent: first, the value of a?.b is ...
BlueRaja - Danny Pflughoeft's user avatar
4 votes
2 answers
2k views

I'm getting a UnassignedReferenceException: The variable _Preset of Foo has not been assigned. even though I'm using the null-conditional operator ?.. My code: // […] myTarget.Preset?.ApplyTo(...
jeromej's user avatar
  • 12k
0 votes
1 answer
829 views

I have following methods: float myMethod(MyObject[][] myList) { float a = 0; if (myListProcessingMethod(myList?.Where(x => x.mySatisfiedCondition()).ToList())) { a = ...
ElConrado's user avatar
  • 1,640
5 votes
3 answers
555 views

The null-conditional operator is very useful when the method belongs to the object in question, but what if the object in question is an argument? For example, can this be shortened? var someList = ...
Zachary's user avatar
  • 326
-3 votes
1 answer
61 views

I'm new in c#. I have a confusion in null conditional operator. in case of string everyone using this line string name = p?.name; but in case of decimal or float decimal? price = p?.price; int? ...
Subhashis Pal's user avatar
8 votes
2 answers
1k views

Consider the following code: IEnumerable<int> xx = null; var tt = xx?.Where(x => x > 2).Select(x => x.ToString()); It assigns null to tt. The question is: why does it work properly? I ...
tsul's user avatar
  • 1,606
1 vote
4 answers
16k views

I'm new to C# but not to programming in general. I am trying to set add some error checking to my program. There are 3 textboxes and I am trying to make it so that if the text box is left blank, it ...
user2328273's user avatar
  • 1,010
0 votes
1 answer
83 views

Having a C# background, I always use the ?. operator when acessing class properties (when using Entity Framework relationships). For example, if I use this and the status class is null, it'll throw ...
Phiter's user avatar
  • 15k
5 votes
1 answer
470 views

We encountered unexpected behavior with the null conditional operator if the variable value is Nothing. The behavior of the following code keeps us a bit confused Dim l As List(Of Object) = ...
Stephael's user avatar
21 votes
3 answers
10k views

I've been playing with C# 6's Null Conditional Operator (more info here). I really like the syntax and I think it makes the code much more readable however I think it is questionable as to what ...
Danny Lager's user avatar
15 votes
1 answer
14k views

Why this code works: if (list?.Any() == true) but this code doesn't: if (list?.Any()) saying Error CS0266 Cannot implicitly convert type 'bool?' to 'bool' So why is it not a language feature making ...
Centro's user avatar
  • 4,042
8 votes
2 answers
586 views

The null conditional operator can be used to skip method calls on a null target. Would the method arguments be evaluated or not in that case? For example: myObject?.DoSomething(GetFromNetwork()); Is ...
boot4life's user avatar
  • 5,422
4 votes
4 answers
282 views

In the following code, one of the two variants does not compile: class C { public decimal DecimalField; } static C GetC() { return new C(); } //Can return null in reality. C c = GetC(); //Get a ...
boot4life's user avatar
  • 5,422
8 votes
4 answers
6k views

I have an event which returns a boolean. To make sure the event is only fired if anyone is listening, i call it using the null-conditional operator (questionmark). However, this means that I have to ...
Jakob Busk Sørensen's user avatar
4 votes
4 answers
1k views

var a = b?.c.d; Shouldn't this expression always give a compile error? If b is null, the null value is propagated through so c will also be null and thus also need this operator. In my understanding ...
codymanix's user avatar
  • 29.6k