C#语法(List、方法的调用)

class GroupSample1
{
    // The element type of the data source.
    public class Student
    {
        public string First { get; set; }
        public string Last { get; set; }
        public int ID { get; set; }
        public List<int> Scores;
    }


    public static List<Student> GetStudents()
    {
         // Use a collection initializer to create the data source. Note that each element
          //  in the list contains an inner sequence of scores.
          List<Student> students = new List<Student>
           {
           new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 72, 81, 60}},
           new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
           new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {99, 89, 91, 95}},
           new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {72, 81, 65, 84}},
           new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {97, 89, 85, 82}} 
           };


        return students;


    }

}



//C#字符串转数组(循环输出)

            string str = "文学笔记|计算机笔记|物流笔记";
            string[] sarr = str.Split('|');
            foreach (var item in sarr)
            {
                Response.Write(item + "<br />");
            }



//C#字符串数组转字符串

           string str = "名字1|代码2|表格3";
            string[] sarr = str.Split('|');

          string arrStr = string.Join(",", sarr);//数组转成字符串
          Response.Write(arrStr);



//类的调用(加static与不加static)

C#语法(List、方法的调用)


//创建的类

C#语法(List、方法的调用)




//=C#时间处理

          DateTime dt;
            System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy/MM/dd";
            dt = Convert.ToDateTime("2011/05/26", dtFormat);
            Response.Write(dt.ToString("yyyyMMdd"));

//////////////////////////////////////////////////////

  • 基本
    • x.m:成员访问
    • x(...):方法和委托调用
    • x[...]:数组和索引器访问
    • x++:后置递增
    • x--:后置递减
    • new T(...):对象和委托创建
    • new T(...){...}:使用初始值设定项的对象创建
    • new {...}:匿名对象初始值设定项
    • new T[...]:数组创建
    • typeof(T):获取 T 的 @System.Type 对象
    • checked(x):在已检查的上下文中计算表达式
    • unchecked(x):在未检查的上下文中计算表达式
    • default(T):获取类型为 T 的默认值
    • delegate {...}:匿名函数(匿名方法)
  • 一元
    • +x:标识
    • -x:取反
    • !x:逻辑取反
    • ~x:按位取反
    • ++x:前置递增
    • --x:前置递减
    • (T)x:将 x 显式转换成类型 T
    • await x:异步等待 x 完成
  • 乘法
    • x * y:乘法
    • x / y:除法
    • x % y:求余
  • 加法
    • x + y:加法、字符串串联、委托组合
    • x – y:减法、委托删除
  • Shift
    • x << y:左移位
    • x >> y:右移位
  • 关系和类型测试
    • x < y:小于
    • x > y:大于
    • x <= y:小于或等于
    • x >= y:大于或等于
    • x is T:如果 x 是 T,返回 true;否则,返回 false
    • x as T:返回类型为 T 的 x;如果 x 的类型不是 T,返回 null
  • 相等
    • x == y:等于
    • x != y:不等于
  • 逻辑“与”
    • x & y:整数型位AND,布尔型逻辑 AND
  • 逻辑 XOR
    • x ^ y:整数型位 XOR,布尔型逻辑 XOR
  • 逻辑“或”
    • x | y:整数型位 OR,布尔型逻辑 OR
  • 条件“与”
    • x && y:仅当 x 不是 false 时,才计算 y
  • 条件“或”
    • x || y:仅当 x 不是 true 时,才计算 y
  • null 合并
    • x ?? y:如果 x 为 null,计算结果为 y;否则,计算结果为 x
  • 条件运算
    • x ? y : z:如果 x 为 true,计算 y;如果 x 为 false,计算 z
  • 赋值或匿名函数
    • x = y:赋值
    • x op= y:复合赋值;支持以下运算符
      • *= /= %= += -= <<= >>= &= ^= |=
    • (T x) => y:匿名函数(lambda 表达式)