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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

C#索引器(二)

發(fā)布時(shí)間:2023/12/18 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#索引器(二) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

索引器允許類和結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引,索引器類似與屬性,不同之處在于他們的訪問器采用參數(shù)。被稱為有參屬性。

簡(jiǎn)單的索引器實(shí)例:

索引器與屬性的比較:

標(biāo)示方式:屬性以名稱來標(biāo)識(shí),索引器以函數(shù)簽名來標(biāo)識(shí)。

索引器可以被重載。屬性則不可以被重載。

屬性可以為靜態(tài)的,索引器屬于實(shí)例成員,不能被聲明為static

多參數(shù)索引器實(shí)例:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Study
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? ScoreIndex s = new ScoreIndex();
??????????? s["張三", 1] = 90;
??????????? s["張三", 2] = 100;
??????????? s["張三", 3] = 80;
??????????? s["李四", 1] = 60;
??????????? s["李四", 2] = 70;
??????????? s["李四", 3] = 50;
??????????? Console.WriteLine("張三課程編號(hào)為1的成績(jī)?yōu)?#xff1a;" + s["張三",1]);
??????????? Console.WriteLine("張三的所有成績(jī)?yōu)?#xff1a;");
??????????? ArrayList temp;
??????????? temp = s["張三"];
??????????? foreach (IndexClass b in temp)
??????????? {
??????????????? Console.WriteLine("姓名:" + b.Name + "課程編號(hào):" + b.CourseID + "分?jǐn)?shù):" + b.Score);
??????????? }
??????????? Console.ReadKey();
??????? }
??? }
??? class IndexClass
??? {
??????? private string _name;
??????? private int _courseid;
??????? private int _score;
??????? public IndexClass(string _name, int _courseid, int _score)
??????? {
??????????? this._name = _name;
??????????? this._courseid = _courseid;
??????????? this._score = _score;
??????? }
??????? public string Name
??????? {
??????????? get { return _name; }
??????????? set { this._name = value; }
??????? }
??????? public int CourseID
??????? {
??????????? get { return _courseid; }
??????????? set { this._courseid = value; }
??????? }
??????? public int Score
??????? {
??????????? get { return _score; }
??????????? set { this._score = value; }
??????? }
??? }
??? class ScoreIndex
??? {
??????? private ArrayList arr;
??????? public ScoreIndex()
??????? {
??????????? arr = new ArrayList();
??????? }
??????? public int this[string _name, int _courseid]
??????? {
??????????? get
??????????? {
??????????????? foreach (IndexClass a in arr)
??????????????? {
??????????????????? if (a.Name == _name && a.CourseID == _courseid)
??????????????????? {
??????????????????????? return a.Score;
??????????????????? }
??????????????? }
??????????????? return -1;
??????????? }
??????????? set
??????????? {
??????????????? arr.Add(new IndexClass(_name, _courseid, value)); //arr["張三",1]=90
??????????? }
??????? }
??????? //重載索引器
??????? public ArrayList this[string _name]
??????? {
??????????? get
??????????? {
??????????????? ArrayList temp = new ArrayList();
??????????????? foreach (IndexClass b in arr)
??????????????? {
??????????????????? if (b.Name == _name)
??????????????????? {
??????????????????????? temp.Add(b);
??????????????????? }
??????????????? }
??????????????? return temp;
??????????? }
??????? }
??? }
}

備注:

所有索引器都使用this關(guān)鍵詞來取代方法名。Class或Struct只允許定義一個(gè)索引器,而且總是命名為this。

索引器允許類或結(jié)構(gòu)的實(shí)例按照與數(shù)組相同的方式進(jìn)行索引。索引器類似于屬性,不同之處在于它們的訪問器采用參數(shù)。

get 訪問器返回值。set 訪問器分配值。

this 關(guān)鍵字用于定義索引器。

value 關(guān)鍵字用于定義由 set 索引器分配的值。

索引器不必根據(jù)整數(shù)值進(jìn)行索引,由您決定如何定義特定的查找機(jī)制。

索引器可被重載。

索引器可以有多個(gè)形參,例如當(dāng)訪問二維數(shù)組時(shí)。

索引器可以使用百數(shù)值下標(biāo),而數(shù)組只能使用整數(shù)下標(biāo):如下列定義一個(gè)String下標(biāo)的索引器
public int this [string name] {...}

屬性和索引器

屬性和索引器之間有好些差別:

類的每一個(gè)屬性都必須擁有唯一的名稱,而類里定義的每一個(gè)索引器都必須擁有唯一的簽名(signature)或者參數(shù)列表(這樣就可以實(shí)現(xiàn)索引器重載)。

屬性可以是static(靜態(tài)的)而索引器則必須是實(shí)例成員。

轉(zhuǎn)載于:https://www.cnblogs.com/jiang_jiajia10/archive/2009/03/13/1410798.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的C#索引器(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。