如何在C# 8.0中使用索引器?

2023年 9月 12日 67.5k 0

如何在C# 8.0中使用索引器?

^运算符 − 它被称为从末尾开始的索引运算符。

它返回相对于序列或集合末尾的索引。

与之前的方法相比,它是找到末尾元素最简洁和最简单的方式。

methods.

company.listEmployees[^2].Name = "使用新语法更改的员工2姓名";

company.listEmployees[^5].Name = "使用新语法更改的员工5姓名";

company.listEmployees[^8].Name = "使用新语法更改的员工8姓名";

示例

public class Employee{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
public class Company{
public List listEmployees;
public Company(){
listEmployees = new List();
listEmployees.Add(new Employee
{ EmployeeId = 1, Name = "SS", Gender = "Male" });
listEmployees.Add(new Employee
{ EmployeeId = 2, Name = "SSS", Gender = "Female" });
listEmployees.Add(new Employee
{ EmployeeId = 3, Name = "SSSS", Gender = "Male" });
listEmployees.Add(new Employee
{ EmployeeId = 4, Name = "EE", Gender = "Female" });
listEmployees.Add(new Employee
{ EmployeeId = 5, Name = "EEEE", Gender = "Female" });
listEmployees.Add(new Employee
{ EmployeeId = 6, Name = "TTT", Gender = "Male" });
listEmployees.Add(new Employee
{ EmployeeId = 7, Name = "FFF", Gender = "Male" });
listEmployees.Add(new Employee
{ EmployeeId = 8, Name = "GGG", Gender = "Male" });
}
public string this[int employeeId]{
get{
return listEmployees.
FirstOrDefault(x => x.EmployeeId == employeeId).Name;
}
set{
listEmployees.
FirstOrDefault(x => x.EmployeeId == employeeId).Name = value;
}
}
}
class Program{
public static void Main(){
Company company = new Company();
Console.WriteLine("Name of Employee with Id = 2: " + company[2]);
Console.WriteLine("Name of Employee with Id = 5: " + company[5]);
Console.WriteLine("Name of Employee with Id = 8: " + company[8]);
System.Console.WriteLine();
Console.WriteLine("Changing names of employees with Id = 2,5,8");
company[2] = "Employee 2 Name Changed";
company[5] = "Employee 5 Name Changed";
company[8] = "Employee 8 Name Changed";
System.Console.WriteLine();
Console.WriteLine("Name of Employee with Id = 2: " + company[2]);
Console.WriteLine("Name of Employee with Id = 5: " + company[5]);
Console.WriteLine("Name of Employee with Id = 8: " + company[8]);
company.listEmployees[^2].Name = "Employee 2 Name Changed using new
Syntax";
company.listEmployees[^5].Name = "Employee 5 Name Changed using new
Syntax";
company.listEmployees[^8].Name = "Employee 8 Name Changed using new
Syntax";
System.Console.WriteLine();
Console.WriteLine("Name of Employee with Id = 2: " +
company.listEmployees[^2].Name);
Console.WriteLine("Name of Employee with Id = 5: " +
company.listEmployees[^5].Name);
Console.WriteLine("Name of Employee with Id = 8: " +
company.listEmployees[^8].Name);
Console.ReadLine();
}
}

登录后复制

输出

Name of Employee with Id = 2: SSS
Name of Employee with Id = 5: EEEE
Name of Employee with Id = 8: GGG
Changing names of employees with Id = 2,5,8
Name of Employee with Id = 2: Employee 2 Name Changed
Name of Employee with Id = 5: Employee 5 Name Changed
Name of Employee with Id = 8: Employee 8 Name Changed
Name of Employee with Id = 2: Employee 2 Name Changed using new Syntax
Name of Employee with Id = 5: Employee 5 Name Changed using new Syntax
Name of Employee with Id = 8: Employee 8 Name Changed using new Syntax

登录后复制

以上就是如何在C# 8.0中使用索引器?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论