C#7.0新语法
  TEZNKK3IfmPf 2023年11月15日 41 0


之前使用out参数需要提前定义变量

//必须先定义i
        int i = 0;

        private static void DoNothing(out int i)
        {
             i= 1;
        }

更新后

//不必先定义result
static void Main(string[] args)
        {

            int.TryParse("a", out int reult);
            Console.WriteLine(reult);
        }

2、元组touple

元组功能在 C# 7.0 及更高版本中可用,它提供了简洁的语法,用于将多个数据元素分组成一个轻型数据结构。

(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.

(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.

var alphabetStart = (Alpha: "a", Beta: "b");//还可以这样声明

元组是可以改变的,类似于类的属性

(string Alpha, string Beta) namedLetters = ("a", "b");
                namedLetters.Alpha = "aa";
                namedLetters.Beta = "bb";
                Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
//output:
//aa,bb

同时方法返回值可以返回元组

static void Main(string[] args)
        {
            (int, int) a = Range();
            Console.Read();
            
        }
static (int, int) Range()
        {
            return (1, 2);
        }

3、弃元

有如下函数,返回值为两个int和两个string类型的元组,但是在实际运行过程中,并不需要元组中所有的元素,仅接受其中的某项元素,称之为弃元。

static (int, int,string,string) Range()
        {
            return (1, 2,"a","b");
        }


static void Main(string[] args)
        {
            var (_, r1, _, r2) = Range();//仅仅接受第1项与第三项
            Console.Read();
            
        }

4、模式

static void Main(string[] args)
        {
            int input = 123;
            int sum = 234;
            if (input is int count)//如果input是int类型,则count=input(count之前未定义)
               sum = count;
            Console.WriteLine();
            Console.Read();
            
        }

另外,模式还可以在Switch中使用

private void Switch(string text)
        {
            int k = 100;
            switch (text)
            {
                case "aaa" when k > 10:   // text="aaa"且k>10才会输出aaa
                    Console.WriteLine("aaa");
                    break;
                case "bbb" when text.Length < 10:  //text="bbbb"且text的长度<10才会输出bbb
                    Console.WriteLine("bbb");
                    break;
                case string s when s.Length > 7://模式 定义变量s,s就是text的值
                    Console.WriteLine(s);
                    break;
            }

5、本地函数

在方法的内部定义一个方法进行调用

public static string LocalFunction(string name)
        {
            return ZhxiToString(name);

            string ZhxiToString(string name)
            {
                return name;
            }
        }

6、默认文本表达式

默认文本表达式是针对默认值表达式的一项增强功能。 这些表达式将变量初始化为默认值。 过去会这么编写:

Func<string, bool> whereClause = default(Func<string, bool>);

现在可以直接=default

Func<string, bool> whereClause1 = default;
string str = default;//这样也是支持的
int i = default;
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: C#8.0新语法 下一篇: Vue常用特性
  1. 分享:
最后一次编辑于 2023年11月15日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   30   0   0 c语言函数
  TEZNKK3IfmPf   2023年11月15日   42   0   0 函数
  TEZNKK3IfmPf   2023年11月14日   24   0   0 函数
  TEZNKK3IfmPf   2023年11月14日   23   0   0 C++函数
  TEZNKK3IfmPf   2023年11月15日   47   0   0 函数语法
  TEZNKK3IfmPf   2023年11月14日   16   0   0 react函数
  TEZNKK3IfmPf   2024年03月29日   56   0   0 函数
  TEZNKK3IfmPf   2023年11月14日   17   0   0 C++函数
  TEZNKK3IfmPf   2023年11月14日   23   0   0 缓存函数
  TEZNKK3IfmPf   2023年11月15日   38   0   0 c语言函数
TEZNKK3IfmPf