-
C# c#–使用字符串作为索引的索引器
c#–使用字符串作为索引的索引器
原文:https://www . geesforgeks . org/c-sharp-indexers-使用字符串作为索引/
先决条件:c# 中属性
索引器允许类或结构的实例像数组一样被索引。通过使用索引器,类将表现得像一个虚拟数组。可以在不显式指定类型或实例成员的情况下设置或检索索引值。索引器类似于属性,只是它们的访问器接受参数。索引器几乎类似于 属性 。索引器和属性的主要区别在于索引器的访问器将接受参数。
使用字符串作为索引的索引器:
这个方法会给你更多的信息,可读性。如果我们想使用字符串作为索引来检索信息。
示例:
ic["username"] = "user12";
ic["password"] = "12345";
这将比下面给出的代码更具可读性。
ic[0] = "user12";
ic[1] = "12345";
语法:
[access_modifier] [return_type] this [argument_list]
{
get
{
// retrieval code or code to get the value
}
set
{
// code for setting value to member
}
在上面的语法中:
- access_modifier :可以是公共的、私有的、受保护的,也可以是内部的。
- return_type :可以是任何有效的 C# 类型。在这种情况下,它将是字符串类型。
- 这个:是指向当前类的对象的关键字。
- 参数 _ 列表:指定索引器的参数列表。
- 获取{ }并设置{ } :这些是访问器。
示例:
C
// C# program to illustrate the Indexers
// Using String as an Index
using System;
// class declaration
class IndexerPOC
{
// class members
private string[] val = new string[4];
// indexer array
private string[] indices={"username","password","email","Book"};
// Indexer declaration
// Here pubic is the modifier
// string is the return type of
// Indexer and "this" is the keyword
// which refer to the calling object.
// having parameters list
public string this[string index]
{
// get Accessor
// retrieving the values
// stored in val[] array
// of strings using string indexer.
get
{
return val[Array.IndexOf(indices,index)];
}
// set Accessor
// setting the value at
// passed i of val
set
{
// value keyword is used
// to define the value
// being assigned by the
// set indexer.
val[ Array.IndexOf(indices,index)] = value;
}
}
}
// Driver Class
class Program {
// Main Method
public static void Main() {
// creating an object of parent class which
// acts as primary address for using Indexer
IndexerPOC ic = new IndexerPOC();
// Inserting values in ic[]
// Here we are using the object
// of class as an array
ic["username"] = "user12";
ic["password"] = "12345";
ic["email"] = "user123@gmail.com";
ic["Book"]="CSHARP";
Console.Write("Printing values stored in objects used as arrays\n");
// printing values
Console.WriteLine("UserName = {0}", ic["username"]);
Console.WriteLine("Password = {0}", ic["password"]);
Console.WriteLine("Email = {0}", ic["email"]);
Console.WriteLine("Book = {0}", ic["Book"]);
}
}
输出:
Printing values stored in objects used as arrays
UserName = user12
Password = 12345
Email = user123@gmail.com
Book = CSHARP
版权属于:月萌API www.moonapi.com,转载请注明出处
本文链接:https://www.moonapi.com/news/28217.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数