c#属性 索引器
来源:互联网 发布:mac 怎么登录千牛 编辑:程序博客网 时间:2024/05/12 00:47
using System;
namespace PropertyIndexerApp
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
//创建一个MyClass实例
MyClass m = new MyClass ();
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
//写、读第一个索引器
m[i*10,j]=i*10+j;
Console.Write("No{0}{1}:{2}",i,j,m[i*10,j]);
}
Console.WriteLine ();
}
for (int i=0;i<m.StrCount ;i++)
{ //读第二个索引器
Console.WriteLine (m[i]);
}
//Set实例属性
m.StrCount = 5;
//Get实例属性
for (int i=0;i<m.StrCount ;i++)
{ //读第二个索引器
Console.WriteLine (m[i]);
}
//读静态属性
Console.WriteLine (MyClass.ClassName );
Console.Write ("Press any key to continue");
Console.ReadLine ();
}
}
class MyClass
{
private const int c_count = 100;
private static int[] intArray = new int[c_count];
//第一个索引器,可读可写,有两个参数
public int this[int index,int offset]
{
get
{
if ((index+offset)>=0&&(index+offset)<c_count)
{
return intArray[index+offset];
}else return 0;
}
set
{
if ((index+offset)>=0&&(index+offset)<c_count)
intArray[index+offset]=value;
}
}
private int m_strCount = 3;
private string[] strArray = {"111","222","333"};
//第二个索引器,只读,一个参数
public string this[int index]
{
get
{
if ((index>=0)&&(index<m_strCount))
{
return strArray[index];
}
else
{
return "NULL";
}
}
}
//实例属性,可读可写
public int StrCount
{
get
{
return m_strCount;
}
set
{
if (value>m_strCount)
{
strArray = new string[value];
for (int i=0;i<value;i++)
{
strArray[i] = String.Format("String No.{0}",i);
}
m_strCount = value;
}
}
}
private static string m_strName = "MyClass";
//一个静态属性,只读
public static string ClassName
{
get
{
return m_strName;
}
}
}
}
- c#属性 索引器
- C#索引器,属性
- C#属性索引和索引器
- C#属性和索引器
- C#索引器与属性
- C#属性和索引器
- C#之属性与索引器
- c#的属性和索引器
- c#中的字段 属性 索引器
- C#中方法,属性与索引器
- C#属性、结构体、索引器、泛型
- C# 索引器与属性的区别
- c#属性和索引
- C# 属性和索引
- C#索引器-索引器与数组属性的比较
- C#索引器-索引器和属性的区别
- C#索引器与索引属性_百度文库
- 细谈C#中的索引器跟属性的区别
- SDP简要解析
- MIME类型大全
- 提高大码率解码器的效率问题
- 【软件自动化测试-QTP实战技能 1】== 使用技巧实例
- debian源配置
- c#属性 索引器
- 人云亦云
- sps pps学习
- 纹章之谜一人攻略——英雄战争篇
- Yanghui Triangle
- AVS-M与H.264(Baseline)视频解码器结构的分析
- (转载)零拷贝技术研究与实现收藏
- ABAP--如何创建自定义打印条码
- linux学习笔记