C#中怎么做到这样的stylevar["charset"]下标为字符串的数组?
2011-03-16 18:40
513 查看
C#中怎么做到这样的stylevar["charset"]下标为字符串的数组?
本文来自: 站长(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/721/268745.htm
问:
在PHP中数组下标可以是字符串,比如:stylevar["charset"]
这样的好处是非常便于记忆,不容易出错。请问在C#中怎么用这样的数据表示方法,谢谢!
______________________________________________________________________________________________
答1:
用索引器
______________________________________________________________________________________________
答2:
namespace Programming_CSharp
{
using System;
// a simplified ListBox control
public class ListBoxTest
{
// initialize the list box with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[256];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the list box
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
private int findString(string searchString)
{
for (int i = 0;i<strings.Length;i++)
{
if (strings[i].StartsWith(searchString))
{
return i;
}
}
return -1;
}
// index on string
public string this[string index]
{
get
{
if (index.Length == 0)
{
// handle bad index
}
return this[findString(index)];
}
set
{
strings[findString(index)] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
private string[] strings;
private int ctr = 0;
}
public class Tester
{
static void Main()
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");
// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
// test the access
string subst = "Universe";
lbt[1] = subst;
lbt["Hel"] = "GoodBye";
// lbt["xyz"] = "oops";
// access all the strings
for (int i = 0;i<lbt.GetNumEntries();i++)
{
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
} // end for
} // end main
} // end tester
} // end namespace
本文来自: 站长(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/721/268745.htm
问:
在PHP中数组下标可以是字符串,比如:stylevar["charset"]
这样的好处是非常便于记忆,不容易出错。请问在C#中怎么用这样的数据表示方法,谢谢!
______________________________________________________________________________________________
答1:
用索引器
______________________________________________________________________________________________
答2:
namespace Programming_CSharp
{
using System;
// a simplified ListBox control
public class ListBoxTest
{
// initialize the list box with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[256];
// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}
// add a single string to the end of the list box
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}
// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}
private int findString(string searchString)
{
for (int i = 0;i<strings.Length;i++)
{
if (strings[i].StartsWith(searchString))
{
return i;
}
}
return -1;
}
// index on string
public string this[string index]
{
get
{
if (index.Length == 0)
{
// handle bad index
}
return this[findString(index)];
}
set
{
strings[findString(index)] = value;
}
}
// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}
private string[] strings;
private int ctr = 0;
}
public class Tester
{
static void Main()
{
// create a new list box and initialize
ListBoxTest lbt =
new ListBoxTest("Hello", "World");
// add a few strings
lbt.Add("Who");
lbt.Add("Is");
lbt.Add("John");
lbt.Add("Galt");
// test the access
string subst = "Universe";
lbt[1] = subst;
lbt["Hel"] = "GoodBye";
// lbt["xyz"] = "oops";
// access all the strings
for (int i = 0;i<lbt.GetNumEntries();i++)
{
Console.WriteLine("lbt[{0}]: {1}",i,lbt[i]);
} // end for
} // end main
} // end tester
} // end namespace
相关文章推荐
- 黑马程序员之C#编程基础学习笔记:将一个字符串数组输出为|分割的形式,比如“梅西|卡卡|郑大世"
- 怎么把字符串"99999999.9"格式化成#99,999,999.9??
- 需要这样一个记录本-7 C#字符串与字节数组相互转换
- FJ的字符串 /*问题描述 FJ在沙盘上写了这样一些字符串: A1="A" A2="ABA" A3="ABACABA" A4="ABACABADABACABA" ... ...
- c# 如何将字符串中用","分开的数字分别存入数组中
- c# 如何将字符串中用","分开的数字分别存入数组中
- 在C#和SQL Server数据库中怎么给DateTime类型赋null值、null和""的区别
- C#中如何去掉字符串结束符"/0"
- "自适应网页设计"到底是怎么做到的?
- 用 Split 函数将有序字符串(如"A1,A2,A3,A4,A5,A6")分散拆开为数组
- 面试题--字符串指针与字符串数组的区别char* st="abc;与char* str[]="abc";的异同
- C++怎么让函数返回固定长度字符串,比如给“zhangsan"返回原字符串加上4各空格,给“lisi"返回"lisi"加6
- C# "125" 转换位“00000125” 左边使用固定字符补充为固定长度的字符串
- "显微镜"下细看字符串常量初始化数组和指针
- 解决从服务器获取的数组是 __NSCFConstantString以及&quot;&quot;没有空格字符串的问题
- C#字符串拼接 "+"连接与Append()方法 速度比较
- C语言根据特定的符号分割字符串 如:字符串"a,bbb,ccc"->数组[a,bbb,ccc]
- 关于c# 引发了"system.indexOutofRangeException"类型异常,该怎么处理
- js中将字符串转换成json--(//highcharts图表里data数据源必须是数组形势的[{name="test1"},{name="test2"}])
- C语言将多个字符串合并为一个字符串例如:数组[a,bbb,ccc]->字符串"a,bbb,ccc"