zoukankan      html  css  js  c++  java
  • C# 运算符

    1、? 可空运算符

    C# 中,有两种类型的变量。
    基本类型和引用类型
    基本类型变量在定义一个变量的时候,不允许将该变量设置为null。
    但是在有的时候,我们又希望该基本类型拥有可以为null的能力。

    所以我们在 该基本类型后面 加? 就可以将该基本类型变成应用类型。
    如 int => int? long => long? 等
    此时 int? 定义的变量,就是引用类型了,可以为null,而不再是一个基本的类型。
    所有的基本类型转换成的引用类型,都有value这个属性,该属性是该引用的值。
    所有 datetime = datetime?.value

    2、??  null 合并运算符

    如果此运算符的左操作数不为 null,则此运算符将返回左操作数;否则返回右操作数
     1         int? x = null;
     2 
     3         // Set y to the value of x if x is NOT null; otherwise,
     4         // if x = null, set y to -1.
     5         int y = x ?? -1;
     6 
     7         // Assign i to return value of the method if the method's result
     8         // is NOT null; otherwise, if the result is null, set i to the
     9         // default value of int.
    10         int i = GetNullableInt() ?? default(int);
    11 
    12         string s = GetStringValue();
    13         // Display the value of s if s is NOT null; otherwise, 
    14         // display the string "Unspecified".
    15         Console.WriteLine(s ?? "Unspecified");

    3、=>  lambda 运算符

    该标记在 lambda 表达式中用来将左侧的输入变量与右侧的 lambda 体分离。 Lambda 表达式是与匿名方法类似的内联表达式,但更加灵活;在以方法语法表示的 LINQ 查询中广泛使用了 Lambda 表达式。 

     

     1 string[] words = { "cherry", "apple", "blueberry" };
     2 
     3 // Use method syntax to apply a lambda expression to each element
     4 // of the words array. 
     5 int shortestWordLength = words.Min(w => w.Length);
     6 Console.WriteLine(shortestWordLength);
     7 
     8 // Compare the following code that uses query syntax.
     9 // Get the lengths of each word in the words array.
    10 var query = from w in words
    11             select w.Length;
    12 // Apply the Min method to execute the query and get the shortest length.
    13 int shortestWordLength2 = query.Min();
    14 Console.WriteLine(shortestWordLength2);
    15 
    16 // Output: 
    17 // 5
    18 // 5

    备注:

    => 运算符具有与赋值运算符 (=) 相同的优先级,并且是右结合运算符。

    可以显式指定输入变量的类型或让编译器推断类型;仍,该变量是强类型在编译时。 当指定类型时,必须将该类型括号中的名称和变量名,如下例所示。

    1 int shortestWordLength = words.Min((string w) => w.Length);

    下面的代码演示如何编写采用两个参数标准查询运算符 Enumerable.Where 的超加载的 lambda 表达式。 由于 lambda 表达式具有多个参数,必须括在括号中的参数。 第二个参数,index,表示当前元素的索引集合中的。 Where 表达式返回长度小于其在数组的索引位置小于的任何字符串。

     1  static void Main(string[] args)
     2  {
     3     string[] digits = { "zero", "one", "two", "three", "four", "five", 
     4             "six", "seven", "eight", "nine" };
     5 
     6     Console.WriteLine("Example that uses a lambda expression:");
     7     var shortDigits = digits.Where((digit, index) => digit.Length < index);
     8     foreach (var sD in shortDigits)
     9     {
    10         Console.WriteLine(sD);
    11     }
    12 
    13     // Compare the following code, which arrives at the same list of short
    14     // digits but takes more work to get there.
    15     Console.WriteLine("
    Example that uses a for loop:");
    16     List<string> shortDigits2 = new List<string>();
    17     for (var i = 0; i < digits.Length; i++)
    18     {
    19         if (digits[i].Length < i)
    20             shortDigits2.Add(digits[i]);
    21     }
    22 
    23     foreach (var d in shortDigits2)
    24     {
    25         Console.WriteLine(d);
    26     }
    27     // Output:
    28     // Example that uses a lambda expression:
    29     // five
    30     // six
    31     // seven
    32     // eight
    33     // nine
    34 
    35     // Example that uses a for loop:
    36     // five
    37     // six
    38     // seven
    39     // eight
    40     // nine
    41 }

    不断补充更新



  • 相关阅读:
    JQuery -- this 和 $(this) 的区别
    js动态生成表格
    Sublime 3 如何使用列编辑模式
    新版Sublime text3注册码被移除的解决办法
    Sublime text 3 格式化HTML/css/js/json代码 插件
    mingw64 构建 Geos
    C++调用Python浅析
    linux下挂载VHD等虚拟磁盘文件
    linux 下Qt WebEngine 程序打包简单记录
    C++ UTF8和UTF16互转代码
  • 原文地址:https://www.cnblogs.com/QQ931697811/p/4350354.html
Copyright © 2011-2022 走看看