What do you think about ??= operator in C#? [closed]
-
23-09-2019 - |
문제
Do you think that C# will support something like ??= operator?
Instead of this:
if (list == null)
list = new List<int>();
It might be possible to write:
list ??= new List<int>();
Now, I could use (but it seems to me not well readable):
list = list ?? new List<int>();
해결책
I have always wanted something like this. I would use it far more often than the ??
by itself.
What I REALLY want, though, is a form of operator that lets you dereference the object only if non null. To replace this:
int count = (list != null)? list.Count : 0
with something like this:
int count = list??.Count : 0
Which would be especially useful for me with long chains of references (bad design, I know), but for example
int count = foo??.bar??.baz??.list??.Count : 0
This isn't currently possible with ??
because you can only say "assign to foo, or an alternative if null" but not "assign to a property of foo, or an alternative if null."
다른 팁
OOB 워크 플로우가 그렇게 할 수 없습니다.다음을 수행하는 SharePoint Designer 워크 플로를 만들어야합니다.
- 부서 재산을 읽으십시오
- 부서의 가치를 확인하는 조건을 추가하십시오
- 블록이있는 경우 각각의 전자 메일을 올바른 사용자에게 보내십시오.
희망이 도움이됩니다.
A trick I found somewhere here on stackoverflow was to do something like this...
private List<string> myList;
public List<string> MyProp
{
get { return myList ?? (myList= new List<string>()); }
}
... you maybe able to use similar lazy eval in your code.
I like it - it is a nice, succinct way to express a lazy-loading expression. Whether or not it gets added to language is another thing altogether - as Eric Lippert loves to point out, new features require significant amounts of work to implement and as such they must contribute a significant net positive to the language in order to be included.
Unless I read your question wrong, that operator does exist in C#: http://msdn.microsoft.com/en-us/library/ms173224(VS.80).aspx