当前位置:网站首页>Use of slice grammar sugar in C #
Use of slice grammar sugar in C #
2022-04-23 03:06:00 【Dotnet cross platform】
Example
First, let's look at an example , There is such an array
string [] lst = new string[] { "1", "2", "3", "4", "5", "6", "7" };
How do we get its last value , The traditional method is written like this ,
string str = lst[lst.Length-1];
How to write slice grammar sugar ?
string str = lst[^1];
If you write like this , There's no difference between the two lines of code , It doesn't show the advantages of slice grammar sugar .
So if we extract index=1,2,3 What about the data? , The traditional practice is like this
var temp = new string【】 { lst[1], lst[2], lst[3] };
Or right linq More familiar students will write like this
var temp = lst.Skip(1).Take(3).ToArray();
Here I'd like to talk about skip and take What does that mean?
•Skip Is to skip the first few elements , Start getting the contents of the subscript value •Take Is to get the values of several elements ,
But if we slice, the operation is too simple
var temp = lst[1..3];
Let's run the code , Why , How can I only get two , Let's look at the official definition ,
End Gets a range that does not contain the end index Index.
What does this mean , That little friend who is familiar with high school mathematics collection should know , there 1..3 It's actually the one in our collection 1 To 3 Left closed right away , It's written like this in mathematics [1,3), Then we need to get the first three elements , It should be written as [1,4)(1 To 4 Left closed right away ), The code to
var temp = lst[1..4];
Through these two examples, you should learn c# Slice grammar sugar in ,
If you like my article , Please pay attention and praise , hope net The ecosystem is getting better and better !
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333877.html
边栏推荐
- 7-11 重排链表 (25 分)
- 微软是如何解决 PC 端程序多开问题的
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- Traversée de l'arbre L2 - 006
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Maui initial experience: Cool
- Blazor University (11) component - replace attributes of subcomponents
- C# 读写二进制文件
- 利用栈的回溯来解决“文件的最长绝对路径”问题
猜你喜欢
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Vs code setting line feed
ASP.NET和ASP.NETCore多环境配置对比
Blazor University (12)组件 — 组件生命周期
Openfeign details show
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
Summary of software test interview questions
准备一个月去参加ACM,是一种什么体验?
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Laravel new route file
随机推荐
Recursion - outputs continuously increasing numbers
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
Laravel8- use JWT
Xamarin效果第二十二篇之录音效果
最通俗易懂的依赖注入之生命周期
交换二叉树中每个结点的左和右
AOT和单文件发布对程序性能的影响
Close the computer port
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
中后二叉建树
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
HLS / chisel practice CORDIC high performance computing complex square root
Using stack to solve the problem of "mini parser"
编码电机PID调试(速度环|位置环|跟随)
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
LNMP MySQL allows remote access
Basic SQL (VIII) data update operation practice
tf. keras. layers. Embedding function
Openfeign timeout setting
建立与遍历二叉树