日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#中索引器是什么_C#中的索引器

發布時間:2025/3/11 C# 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#中索引器是什么_C#中的索引器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c#中索引器是什么

An Indexer is a special feature of C# to use an object as an array. If you define an indexer in a class then it will behave like a virtual array. Indexer is also known as smart array in C#. It is not a compulsory or essential part of OOPS.

索引器是C#的一項特殊功能,可以將對象用作數組。 如果在類中定義索引器,則其行為將類似于虛擬數組。 索引器在C#中也稱為智能數組。 它不是OOPS的強制性或必要部分。

It is also known as indexed property. It is a class property that allows to access data members using a feature of array.

也稱為索引屬性 。 它是一個類屬性,允許使用數組功能訪問數據成員。

We use this object to implement indexer in class.

我們使用該對象在類中實現索引器。

Syntax:

句法:

<Modifier> <return type> this[parameter list] {get{//write code here}set{//write code here} }

Example

using System; using System.Collections; namespace ConsoleApplication1 {class Names{static public int len=10;private string []names = new string[len] ;public Names(){for (int loop = 0; loop < len; loop++)names[loop] = "N/A";}public string this[int ind]{get{string str;if (ind >= 0 && ind <= len - 1)str = names[ind];elsestr = "...";return str;}set{if (ind >= 0 && ind <= len - 1)names[ind]=value;}}}class Program{static void Main(){Names names = new Names();names[0] = "Duggu";names[1] = "Shaurya";names[2] = "Akshit";names[3] = "Shivika";names[4] = "Veer";for (int loop = 0; loop < Names.len; loop++){Console.WriteLine(names[loop]);}}} }

Output

輸出量

DugguShauryaAkshitShivikaVeerN/AN/AN/AN/AN/A

Important point regarding indexers:

關于索引器的要點:

  • We use this keyword to implement indexer.

    我們使用此關鍵字來實現索引器。

  • Indexer can be overloaded.

    索引器可能過載。

  • Indexer cannot be static.

    索引器不能是靜態的。

Read more: Indexer overloading in C#

: C#中的索引器重載

翻譯自: https://www.includehelp.com/dot-net/indexers-in-c-sharp.aspx

c#中索引器是什么

總結

以上是生活随笔為你收集整理的c#中索引器是什么_C#中的索引器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。