Here's a question for the c# operator.

Asked 2 years ago, Updated 2 years ago, 41 views

Hi, everyone. I am currently a VS2013 user.

During open source analysis

public int NumColumns => Bounds?NumColumns ?? 0;

I found a part that declares in form, but I'm confused because it seems to be supported from VS 2015 or later

I know what it means individually, but I'm not sure which operator should be applied first and which form.

How can I use it in vs2013 (.net 6.0)?

c# .net

2022-09-22 15:52

2 Answers

Let's go back to the past.

Let's subtract =>.

public int NumColumns => Bounds?.NumColumns ?? 0;

public int NumColumns {
   get { return Bounds?.NumColumns ?? 0; }
}

=> is also used as an anonymous function (called lambda), but you can abbreviate it when the property has only getter.

Next time, let's take out ?. The ?. operator approaches the member only when it is not null, and returns null if null.

2, 3 See also links

public int NumColumns {
   get { 
      var result; // This is not allowed. For convenience, I used var
      if (Bounds == null) {
          result = null;
      }
      else {
          result = Bounds.NumColumns ?? 0;
      }
      return result;
   }
}

It is called null coercion. If the left side is null, write the value to the right. Let's take it out.

public int NumColumns {
   get { 
      var result; // This is not allowed. For convenience, I used var
      if (Bounds == null) {
          result = null;
      }
      else {
          if (Bounds.NumColumns != null) {
              result = Bounds.NumColumns;
          }
          else {
              result = 0;
          }
      }
      return result;
   }
}

The C# version and the dotnet version are separate.

Higher versions of the dotnet allow you to write more dotnet APIs, and higher versions of the C# allow you to write more grammatical functions.

I mean, by writing Visual Studio 2015, you can create a project for Dotnet 2.0. In this case, you can use these features that do not require a higher version of the parent version C#'s grammar. I think VS 2015 was C#6? I'm confused.

Dotnet Framework 6.0 hasn't come out yet, so I think the version you wrote down is the C# version.

In other words, you can't use C# 6.0 grammar in VS 2013, but if you develop it in VS 2015 without project modification, you can use these grammar.


2022-09-22 15:52

It comes out right away if you refer to the official site reference.

=> is a lambda expression, as you know, ?? is called the null merge operator. If Bounds.NumColumns is not null, NumColumns is Bounds.NumColumns, and Null means 0.


2022-09-22 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.