//声明为密封类防止被继承 public sealed class StringHelper { //添加私有无参构造函ˉ数防止被实例化,如果不添加私有构造函数 //会自动生成共有无参构造函数 private StringHelper(){}; public static int StringToInt32(string input) { int result=0; Int32.TryParse(input, out result); return result; } }
C#2中可以使用静态类来实现:
1 2 3 4 5 6 7 8 9
public static class StringHelper { public static int StringToInt32(string input) { int result=0; Int32.TryParse(input, out result); return result; } }
public class ClassDemo{} namespace NameSpaceDemo1 { public class ClassDemo{} public class Test { static void Main() { Console.WriteLine(typeof(ClassDemo)); //NameSpaceDemo1.ClassDemo Console.WriteLine(typeof(global::ClassDemo)); //ClassDemo Console.ReadLine(); } } }
extern alias FirstDemo; extern alias SecondDemo; using System; namespace NameSpaceDemo1 { public class ClassDemo{} public class Test { static void Main() { Console.WriteLine(typeof(FirstDemo::Demo.Class1)); Console.WriteLine(typeof(SecondDemo::Demo.Class1)); Console.ReadLine(); } } }
public class Test { static void Main() { Person arrPerson = new Person("oec2003","oec2004","oec2005"); foreach (string p in arrPerson) { Console.WriteLine(p); } Console.ReadLine(); } } public class Person:IEnumerable { public Person(params string[] names) { _names = new string[names.Length]; names.CopyTo(_names, 0); } public string[] _names; public IEnumerator GetEnumerator() { return new PersonEnumerator(this); } private string this[int index] { get { return _names[index]; } set { _names[index] = value; } } } public class PersonEnumerator : IEnumerator { private int _index = -1; private Person _p; public PersonEnumerator(Person p) { _p = p; } public object Current { get { return _p._names[_index]; } } public bool MoveNext() { _index++; return _index < _p._names.Length; } public void Reset() { _index = -1; } }
public class Test { static void Main() { Person arrPerson = new Person("oec2003","oec2004","oec2005"); foreach (string p in arrPerson) { Console.WriteLine(p); } Console.ReadLine(); } } public class Person:IEnumerable { public Person(params string[] names) { _names = new string[names.Length]; names.CopyTo(_names, 0); } public string[] _names; public IEnumerator GetEnumerator() { foreach (string s in _names) { yield return s; } } }
private void btnTest_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(delegate() { for (int i = 0; i < 100; i++) { Thread.Sleep(100); this.Invoke(new Action(delegate() { label1.Text = i + "/100"; })); } })); thread.Start(); }
★C#2对数据类型的转换提供了TryParse,可以更好的进行容错,看下面代码:
1 2 3 4 5 6 7 8
static void Main() { int a = 0; string b = "30", c = "aaa"; int.TryParse(b, out a); Console.WriteLine(a);//30 int.TryParse(c, out a); Console.WriteLine(a);//0 Console.ReadLine(); }
public static class DynamicLinqExpressions { public static Expression<Func<T, bool>> True<T>() { return f => true; } public static Expression<Func<T, bool>> False<T>() { return f => false; }
public class UserInfo { public string Name { get; set; } public int Age { get; set; } } public class Test { static void Main() { List<UserInfo> users = new List<UserInfo>() { new UserInfo{Name="oec2003",Age=20}, new UserInfo{Name="oec2004",Age=21}, new UserInfo{Name="oec2005",Age=22} }; IEnumerable<UserInfo> selectedUser = from user in users where user.Age > 20 orderby user.Age descending select user; foreach (UserInfo user in selectedUser) { Console.WriteLine("姓名:"+user.Name+",年龄:"+user.Age); } Console.ReadLine(); } }
public class Test { static void Main() { Console.WriteLine(GetUserInfo()); //姓名:ooec2003,年龄:30 Console.WriteLine(GetUserInfo("oec2004", 20));//姓名:ooec2004,年龄:20 Console.ReadLine(); } public static string GetUserInfo(string name = "oec2003", int age = 30) { return "姓名:" + name + ",年龄:" + age.ToString(); } }