当前位置:网站首页>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
边栏推荐
猜你喜欢

Detailed log display of openfeign call

树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
![Introduction to ACM [inclusion exclusion theorem]](/img/3a/9bc2a972d7587aab51fceb8cd2b9bd.png)
Introduction to ACM [inclusion exclusion theorem]

最通俗易懂的依赖注入之生命周期

Q-Learning & Sarsa

tf. keras. layers. Timedistributed function

PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type

宁德时代地位不保?

腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]
随机推荐
使用两种方法来解决“最大回文数乘积”问题
svg标签中利用<polygon/>循环数组绘制多边形
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
MAUI初体验:爽
先中二叉建树
編碼電機PID調試(速度環|比特置環|跟隨)
The space between the left and right of the movie ticket seats is empty and cannot be selected
宁德时代地位不保?
A set of C interview questions about memory alignment. Many people make mistakes!
Miniapi of. Net7 (special section): NET7 Preview3
LNMP MySQL allows remote access
Openfeign timeout setting
Traversée de l'arbre L2 - 006
C#中切片语法糖的使用
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
.NET7之MiniAPI(特别篇):.NET7 Preview3
中后二叉建树
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持