C# 一点点

字符串:"abcbeee",要求结果如下"
a:1
b:2
c:1
e:3
string str1 = "abcbeee";

//1 hashtable
Hashtable ht = new Hashtable();
for (int i1 = 0; i1 < str1.Length; i1++)
{
string v = str1[i1].ToString();
if (ht.Contains(v))
{
ht[v]
= int.Parse(ht[v].ToString()) + 1;
}
else
{
ht.Add(v,
1);
}

char[] arr = str1.ToCharArray();  

//2 dictionary<>
Dictionary<string, int> dict = new Dictionary<string, int>();
foreach (char c in arr)
{
if (dict.ContainsKey(c.ToString())) dict[c.ToString()]++;
else dict.Add(c.ToString(), 1);
}

// 3 linq
var q = from x in arr group x by x into Y select new { Y.Key, ct = Y.Count() };

//4 linq
var lis = str1.GroupBy(s => s).Select(g => new { g.Key, Count = g.Count() });

输出数组内容

代码
string[] names = {"wang","zhang","liu","zhao","li" };

Console.WriteLine(
"\n1 by method");
Array.ForEach(names, Console.Write);
Console.WriteLine(
"\n2 by anonymous method");
Array.ForEach(names,
delegate(string name) { Console.Write(name+"-"); });
Console.WriteLine(
"\n3 by lamada");
Array.ForEach(names, name
=> Console.Write(name+".."));
Console.WriteLine(
"\n4 by action 1");
Array.ForEach(names,
new Action<string>(Console.Write));
Console.WriteLine(
"\n5 by action 2");
Array.ForEach(names,
new Action<string>(delegate(string name) { Console.Write(name + "**"); }));

datagridview 序号列

代码
'数据绑定
Private Sub BindData()
Dim bll As New WireBLL.Product
Dim dt As DataTable = bll.GetList(Me.capacity, currentPage)
Dim column As New DataColumn
column.ColumnName
= "AutoId"
column.AutoIncrement
= True
column.AutoIncrementSeed
= (Me.currentPage - 1) * Me.capacity + 1
column.AutoIncrementStep
= 1
Dim dtCol As New DataTable
dtCol.Columns.Add(column)
dtCol.Merge(dt)
Me.DataGridView1.DataSource = dtCol
If Me.DataGridView1.CurrentRow IsNot Nothing Then
Me.DataGridView1.CurrentRow.Selected = False
End If
End Sub

校验相关

代码
private void button1_Click(object sender, EventArgs e)
{
vali(
this.textBox1, string.IsNullOrEmpty); //""判断为空
vali(this.textBox2, IsZero); //00判断为空
vali(this.textBox3, str => str == "0.000"); //0.000判断为空
vali(this.textBox4, delegate(string str) { return str == "0"; });//0判断为空
}

private bool IsZero(string str)
{
return str == "00";
}

private void vali(Control con, Predicate<string> pre)
{
if (pre(con.Text))
{
MessageBox.Show(
string.Format("{0} cann't be empty", con.Name));
}
}

原文地址:https://www.cnblogs.com/cnbwang/p/1909698.html