当前位置:网站首页>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
边栏推荐
- Two methods are used to solve the "maximum palindrome product" problem
- Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
- .Net Core 限流控制-AspNetCoreRateLimit
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- tf. keras. layers. Inputlayer function
- Simple example of using redis in PHP
- Judge whether there is a leap year in the given year
- Openfeign service call
- Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
- svg标签中利用<polygon/>循环数组绘制多边形
猜你喜欢

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比

What kind of experience is it to prepare for a month to participate in ACM?

MAUI初体验:爽

利用栈的回溯来解决“文件的最长绝对路径”问题

Onenet connection process

ASP.NET 6 中间件系列 - 自定义中间件类

Guangcheng cloud service can fill in a daily report regularly every day

編碼電機PID調試(速度環|比特置環|跟隨)

Detailed log display of openfeign call

The backtracking of stack is used to solve the problem of "the longest absolute path of file"
随机推荐
Er and eer models
Introduction to ACM [TSP problem]
TP5 multi conditional where query (using PHP variables)
宁德时代地位不保?
svg标签中利用<polygon/>循环数组绘制多边形
[ncnn] - the meaning of - 23300 in param
编码电机PID调试(速度环|位置环|跟随)
tf. keras. layers. Embedding function
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Use DFS to solve the problem of "number of dictionary rows"
7-11 rearrange the linked list (25 points)
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
最通俗易懂的依赖注入之服务容器与作用域
Using stack to solve the problem of "mini parser"
【鉴权/授权】自定义一个身份认证Handler
[Euler plan question 13] sum of large numbers
AOT和单文件发布对程序性能的影响
Use split to solve the "most common words" problem
Depth deterministic strategy gradient (ddpg)
tf. keras. layers. Timedistributed function