关于索引器的 问题 在线等

来源:百度知道 编辑:UC知道 时间:2024/05/13 15:11:02
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

class IndexerClass
{
private int[] arr = new int[100];
public int this[int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
{
return 0;
}
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
arr[index] = value;
}
}
}
}

class MainClass
{
static void Main()
{

0和1代表的是后面的
System.Console.WriteLine("Element #{0} = {1}", i, test[i]);

第1个变量i和第2个变量test[i]
数组和字符串都是从第0位开始的,所以就是从0开始了,是必须的。

{}是转译字符!语言的特定格式!
0和1代表索引!要输入的第一个字符的占位符!

System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
"Element #{0} = {1}"相当于一个模板,
{0}代表其后的第一个参数,也就是i
{1}代表其后的第二个参数,也就是test[i]
等效于"Element #" + i.ToString() + " = " + test[i].ToString()
序号按0,1,2这样排下去
比如有3个参数的情况:
System.Console.WriteLine("{0} = {1} + {2}", 3, 1,2);
3=1+2