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();
}
}
}
C#小白在线乞求大佬解释一下这一整段代码,这段代码对我非常重要,希望有大佬能帮我解析一下
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- dark9spring 2021-03-26 13:16关注
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(); } } }
应该就是这样了,本人只是学了一个月左右吧,如果有不对的地方请大佬指出,欢迎批评,题主有不懂的问我我尽量给你解答一下
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 对于相关问题的求解与代码
- ¥15 ubuntu子系统密码忘记
- ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作
- ¥15 求NPF226060磁芯的详细资料
- ¥15 使用R语言marginaleffects包进行边际效应图绘制