当前位置:网站首页>C#中切片语法糖的使用
C#中切片语法糖的使用
2022-04-23 03:02:00 【dotNET跨平台】
例子
首先我们看这样一个例子,有这样一个数组
string [] lst = new string[] { "1", "2", "3", "4", "5", "6", "7" };
我们怎么获取它的最后一个值, 传统方法是这样写的,
string str = lst[lst.Length-1];
那切片语法糖怎么写呢?
string str = lst[^1];
如果这样写,那这两行代码其实没什么区别,没有体现出切片语法糖的优势。
那如果我们提取index=1,2,3的数据呢, 传统做法是这样的
var temp = new string【】 { lst[1], lst[2], lst[3] };
或者对linq比较熟悉的同学会这样写
var temp = lst.Skip(1).Take(3).ToArray();
在这里我还是讲一下skip和take是什么意思
•Skip是跳过第几个元素之后,开始取得下标值的内容•Take 是获取几个元素的值,
但是如果我们切片操作就太简单了
var temp = lst[1..3];
我们跑一下代码,咦,怎么只获取到两个,我们看一下官方的定义,
End 获取一个表示范围的不包含结束索引的 Index。
这个是什么意思呢,那对高中数学集合还比较熟悉的小伙伴应该知道,这里的1..3其实就是我们集合中的1到3左闭右开, 数学中是这样写的[1,3), 那我们要获取前三个元素,就应该写成[1,4)(1到4左闭右开),代码改成
var temp = lst[1..4];
通过这两个例子大家应该学会了c#中的切片语法糖吧,
如果大家喜欢我的文章,还麻烦给个关注并点个赞, 希望net生态圈越来越好!
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124287007
边栏推荐
- JS relearning
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- [format] simple output (2)
- Deep q-network (dqn)
- C# 11 的这个新特性,我愿称之最强!
- tf. keras. layers. Embedding function
- Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
- Publish to NPM?
- Sonic cloud real machine tutorial
猜你喜欢
Huawei machine test question -- deformation of hj53 Yang Hui triangle
Deep q-network (dqn)
Android high-level interview must ask: overall business and project architecture design and reconstruction
[software testing] understand the basic knowledge of software testing
Niuke white moon race 6 [solution]
Traversée de l'arbre L2 - 006
Encapsulate components such as pull-down menu based on ele
C# 11 的这个新特性,我愿称之最强!
Innovation and management based on Scrum
Domestic lightweight Kanban scrum agile project management tool
随机推荐
@Usage and difference between mapper and @ repository
Jz76 delete duplicate nodes in linked list
Planning code ROS migration POMDP prediction planning (I)
Chapter IV project cost management of information system project manager summary
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
Typescript Learning Guide
How to count the number of all files in a directory under win10 system
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Winsock programming interface experiment: implementation of ipconfig
Opencv fills the rectangle with a transparent color
How to write the expected salary on your resume to double your salary during the interview?
[ncnn] - the meaning of - 23300 in param
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
ROP Emporium x86_ 64 7 ~ 8 questions
Linux Redis——Redis 数据库缓存服务
[software testing] understand the basic knowledge of software testing
REINFORCE
Chapter VI project information management system summary
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Close the computer port