C#索引器
索引器允許類別或結構的實例按照與陣列相同的方式進行索引取值,索引器與屬性類似,不同的是索引器的存取是帶參的。
索引器和陣列比較:
(1)索引器的索引值(Index)類型不受限制
(2)索引器允許重載
(3)索引器不是一個變數
索引器和屬性的不同點
(1)屬性以名稱來標識,索引器以函數形式標識
(2)索引器可以被重載,屬性不可以
(3)索引器不能宣告為static,屬性可以
一個簡單的索引器範例
using System; using System.Collections; public class IndexerClass { private string[] name = new string[2]; //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象 public string this[int index] { //实现索引器的get方法 get { if (index < 2) { return name[index]; } return null; } //实现索引器的set方法 set { if (index < 2) { name[index] = value; } } } } public class Test { static void Main() { //索引器的使用 IndexerClass Indexer = new IndexerClass(); //“=”号右边对索引器赋值,其实就是调用其set方法 Indexer[0] = "张三"; Indexer[1] = "李四"; //输出索引器的值,其实就是调用其get方法 Console.WriteLine(Indexer[0]); Console.WriteLine(Indexer[1]); } }
以字串作為下標,對索引器進行存取
public class IndexerClass { //用string作为索引器下标的时候,要用Hashtable private Hashtable name = new Hashtable(); //索引器必须以this关键字定义,其实这个this就是类实例化之后的对象 public string this[string index] { get { return name[index].ToString(); set { name.Add(index, value); } } } public class Test { static void Main() { IndexerClass Indexer = new IndexerClass(); Indexer["A0001"] = "张三"; Indexer["A0002"] = "李四"; Console.WriteLine(Indexer["A0001"]); Console.WriteLine(Indexer["A0002"]); } }
索引器的重載
public class IndexerClass { private Hashtable name = new Hashtable(); //1:通过key存取Values public string this[int index] { get { return name[index].ToString(); } set { name.Add(index, value); } } //2:通过Values存取key public int this[string aName] { get { //Hashtable中实际存放的是DictionaryEntry(字典)类型,如果要遍历一个Hashtable,就需要使用到DictionaryEntry foreach(DictionaryEntry d in name) { if (d.Value.ToString() == aName) { return Convert.ToInt32(d.Key); } } return -1; } set { name.Add(value, aName); } } } public class Test { static void Main() { IndexerClass Indexer = new IndexerClass(); //第一种索引器的使用 Indexer[1] = "张三";//set访问器的使用 Indexer[2] = "李四"; Console.WriteLine("编号为1的名字:" + Indexer[1]);//get访问器的使用 Console.WriteLine("编号为2的名字:" + Indexer[2]); Console.WriteLine(); //第二种索引器的使用 Console.WriteLine("张三的编号是:" + Indexer["张三"]);//get访问器的使用 Console.WriteLine("李四的编号是:" + Indexer["李四"]); Indexer["王五"] = 3;//set访问器的使用 Console.WriteLine("王五的编号是:" + Indexer["王五"]); } }
更多C#索引器相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。
