using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 索引器的使用
{
class Program
{
private double[,] elements;
private int row = 1;
private int col = 1;
public int Row
{
get
{
return row;
}
}
public int Col
{
get
{
return col;
}
}
public double[] this[int r]
{
get
{
if(r>=row||r<0)
{
throw new IndexOutOfRangeException();
}
double[] vector = new double[col];
for(int i=0;i<col;i++)
{
vector[i] = elements[r, i];
}
return vector;
}
set
{
if(r>=row||r<0)
{
throw new IndexOutOfRangeException();
}
for(int i=0;i<col;i++)
{
elements[r, i] = value[i];
}
}
}
public double this[int r,int c]
{
get
{
if(r>=row||r<0||c>=col||c<0)
{
throw new IndexOutOfRangeException();
}
return elements[r, c];
}
set
{
if(r>=row||r<0||c>=col||c<0)
{
throw new IndexOutOfRangeException();
}
elements[r, c] = value;
}
}
public void Chuli ()
{
row = 1;
col = 1;
elements = new double[row, col];
}
public void Chuli(int row,int col)
{
this.row = row;
this.col = col;
elements = new double[row, col];
}
static void Main(string[] args)
{
const int ROW= 8;
const int COL = 8;
Program chuli = new Program();
chuli.Chuli(ROW, COL);
for (int i=0;i<chuli.Row;i++)
{
for(int j=0;j<chuli.Col;j++)
{
chuli[i, j] = i * j;
}
}
for(int i=0;i<chuli.Row;i++)
{
for(int j=0;j<chuli.Col;j++)
{
Console.Write("{0,4}", chuli[i, j]);
}
Console.WriteLine();
Console.ReadKey();
}
double[] vector = chuli[4];
for(int j=0;j<vector.Length;j++)
{
Console.Write("{0,4}", vector[j]);
}
Console.ReadKey();
}
}
}
using System;
namespace 索引器的使用
{
class Program
{
private double[,] elements; //定义二维数组
private int row = 1; //定义行
private int col = 1; //列
public int Row //get方法获取值:属性
{ //同一个类里能访问到row和col,还写了个只读属性(问号脸)
get
{
return row;
}
}
public int Col //get方法获取值:属性
{
get
{
return col;
}
}
public double[] this[int r] //声明返回一维double数组的索引器,索引为r
{
get
{
if (r >= row || r < 0) //如果索引元素异常,抛出异常
{
throw new IndexOutOfRangeException();
}
double[] vector = new double[col];
for (int i = 0; i < col; i++)
{
vector[i] = elements[r, i]; //赋值给vector
}
return vector; //返回vector数组
}
set
{
if (r >= row || r < 0) //如果索引元素异常,抛出异常
{
throw new IndexOutOfRangeException();
}
for (int i = 0; i < col; i++)
{
elements[r, i] = value[i]; //设置elements[r, i]的值
}
}
}
public double this[int r, int c] //声明返回值为double的索引器。索引为r,c
{
get
{
if (r >= row || r < 0 || c >= col || c < 0) //如果索引元素异常,抛出异常
{
throw new IndexOutOfRangeException();
}
return elements[r, c]; //否则返回elements[r, c]元素
}
set
{
if (r >= row || r < 0 || c >= col || c < 0)
{
throw new IndexOutOfRangeException();
}
elements[r, c] = value; //设置elements[r, c]的值
}
}
//public void Chuli() //没用到可注释掉,其实是测试异常的方法
//{
// row = 1;
// col = 1;
// elements = new double[row, col];
//}
public void Chuli(int row, int col) //函数赋值兼声明数组长度
{
this.row = row;
this.col = col;
elements = new double[row, col];
}
static void Main(string[] args)
{
const int ROW = 8;
const int COL = 8;
Program chuli = new Program(); //实例化program对象为chuli
chuli.Chuli(ROW, COL); //调用Chuli函数
for (int i = 0; i < chuli.Row; i++) //遍历行
{ //
for (int j = 0; j < chuli.Col; j++) //遍历列
{ //
chuli[i, j] = i * j; //赋值给数组元素
}
}
for (int i = 0; i < chuli.Row; i++) //遍历行
{ //
for (int j = 0; j < chuli.Col; j++) //遍历列
{ //
Console.Write("{0,4}", chuli[i, j]); //输出数组元素
}
Console.WriteLine();
Console.ReadKey();
}
double[] vector = chuli[4]; //声明vector一维数组,把chuli第五行元素赋值给vector
for (int j = 0; j < vector.Length; j++)
{
Console.Write("{0,4}", vector[j]); //输出vector元素
}
Console.ReadKey();
}
}
}
应该就是这样了,本人只是学了一个月左右吧,如果有不对的地方请大佬指出,欢迎批评,题主有不懂的问我我尽量给你解答一下
能运行下去?会抛出这个异常的吧