当前位置:网站首页>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
边栏推荐
- Publish to NPM?
- Liunx foundation - zabbix5 0 monitoring system installation and deployment
- Wepy learning record
- Introduction to ACM [TSP problem]
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- Shell script learning -- practical case
- Essential qualities of advanced programmers
- Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is
- [software testing] understand the basic knowledge of software testing
- Codeforces round 784 (Div. 4) (a - H)
猜你喜欢

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Encapsulate components such as pull-down menu based on ele

Processes and threads

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)

Deep q-network (dqn)

HLS / chisel practice CORDIC high performance computing complex square root

How to write the expected salary on your resume to double your salary during the interview?
![[hcip] detailed explanation of six LSAS commonly used by OSPF](/img/31/3b92d42d16a056bf9db9e24471cefd.jpg)
[hcip] detailed explanation of six LSAS commonly used by OSPF

Summary of software test interview questions

tf. keras. layers. Conv? D function
随机推荐
B blocks of the 46th ICPC Asian regional competition (Kunming)
Linux Redis——Redis 数据库缓存服务
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
[hcip] detailed explanation of six LSAS commonly used by OSPF
First knowledge of C language ~ branch statements
JZ35 replication of complex linked list
Linux redis - redis database caching service
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
《信息系统项目管理师总结》第五章 项目质量管理
Android high-level interview must ask: overall business and project architecture design and reconstruction
【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
《信息系统项目管理师总结》第六章 项目人力资源管理
Kubernetes - detailed explanation of pod
Close the computer port
Classification of technology selection (2022)
Winsock programming interface experiment: implementation of ipconfig
Navicat premium import SQL file
Airtrack cracking wireless network password (Dictionary running method)
tf. keras. layers. Density function