zoukankan      html  css  js  c++  java
  • C#基础知识

    同一命名空间下的两个类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HelloWorld
    {
        class A
        {
            private int a;
            public A(int x) { a = x;  }
            public void show() { Console.WriteLine(a); }
        }
    
        
        class Program
        {
            static void Main(string[] args)
            {
                A a = new A(100);
                a.show();
                Console.ReadKey();
            }
        }
       
    
    }
    View Code

    基础数据类型

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HelloWorld
    {  
        class Program
        {
            static void Main(string[] args)
            {
                sbyte sbt = 127;  //8位有符号数,128超出范围,强制转换都不行
                Console.WriteLine(sbt);
                short sht = 10000; //16位有符号数
                int a = 1000000;   //32位有符号数
                long b = 100000000000; //64位有符号数
                //无符号整型有: byte ushort uint ulong
                char c = 'A';   //字符型,而且无法隐式转换,可以显示转换
                Console.WriteLine(c);
                //浮点型有: float(32位) double(64位) decimal(128位)
                decimal dem = 1e-9M; //不能隐式转换,一般的数默认double, 加后缀M或者强制转换
                Console.WriteLine(dem);
                bool flag = true;  //bool型
                Console.WriteLine(flag);
                Console.ReadKey();
            }
        } 
    }
    View Code

    结构体

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HelloWorld
    {  
        struct A
        {
            int a,b;
            public A(int a=0,int b = 0)//跟C语言不同,有访问级别
            {
                this.a = a;
                this.b = b;
            }
            public void show() { Console.WriteLine(a*b);  }
          
        }
        class Program
        {
            static void Main(string[] args)
            {
                A obj =new A(12,13);
                obj.show();
                Console.ReadKey();
            }
        } 
    }
    View Code

    string字符串

    string s = "hello world";
    Console.WriteLine(s);
    View Code

    强制转换和格式

    string s = Console.ReadLine();
                double a = double.Parse(s); //强制转换
                Console.WriteLine(a*1.7); //默认最多保留14位
                s = String.Format("半径={0:f2}
    周长={1:f2}", a, 2 * 3.14* a);
                Console.WriteLine(s);
    View Code

    空类型值

    int? a;  //表明一个值可以为空类型
                a = null;
                int b;
                if (a.HasValue) b = (int)a; //不能隐式转换
                else b = 1;
                Console.WriteLine(b);
    View Code

    字符型

    char ch = 'A'; 
    char ch = '101';      // 用8进制数表示ASCII字符,最多3位
    char ch = 'x41';      //用2位16进制数表示ASCII字符
    char ch = 'x0041';  //用低2位16进制数表示ASCII字符
    char ch ='u0041';   //Unicode字符,必须4位16进制数
    View Code

    char数据常用方法

    char c =(char) Console.Read();
                Console.WriteLine(c);
                if (char.IsDigit(c)) Console.WriteLine(c);
    //char.方法 的格式
    View Code

    string用法以及比较

    char[] ch = { 'a', 'a', 'a' };
                string s1 = new string(ch);
                string s2 = "aa" + "a";
                Console.WriteLine(s1==s2);
                Console.WriteLine(s1.Equals(s2));
                Console.WriteLine(s1.CompareTo(s2));
    View Code

    @的用法

    C#字符串可以@开头,并用双引号引起来:√ 
                string s3 = @"c:myFoldermyFile.txt";
                若要在一个用 @ 引起来的字符串中包括一个双引号,则应使用两个双引号:
                例如: "You!" cried the captain.
                则用: @"""You!"" cried the captain."
    View Code

    常见数值输出格式

     C 或 c
    货币
    Console.Write("{0:C}", 2.5);   //$2.50
    Console.Write("{0:C}", -2.5); //($2.50)
    D 或 d
    十进制数
    Console.Write("{0:D5}", 25);   //00025
    
    E 或 e
    科学型
    Console.Write("{0:E}", 250000);   //2.500000E+005
    
    F 或 f
    固定点
    Console.Write("{0:F2}", 25);   //25.00
    Console.Write("{0:F0}", 25);   //25
    
    G 或 g
    常规
    Console.Write("{0:G}", 2.5);   //2.5
    
    N 或 n
    数字
    Console.Write("{0:N}", 2500000);   //2,500,000.00
    
    X 或 x
    十六进制
    Console.Write("{0:X}", 250);   //FA
    Console.Write("{0:X}", 0xffff);   //FFFF
    View Code

    类型转换

    string s = "123";
                int a = System.Convert.ToInt32(s);
                Console.WriteLine(a);
                s = "2016/02/27";
                DateTime dt = System.Convert.ToDateTime(s);
                Console.WriteLine(dt);
    View Code

    日期类型

    DateTime date1 = DateTime.Now;     //获得当前系统日期和时间
    DateTime date2 = new DateTime(2014,10,1);    //年月日
    Console.WriteLine(date1);   //日期和时间
    Console.WriteLine(date2.ToLongDateString());  //长日期 2014年10月1日
    Console.WriteLine(date2.ToShortDateString()); //短日期 2014/10/1
    Console.WriteLine(date1.Year);   //年
    Console.WriteLine(date1.Hour);   //时
    Console.WriteLine(date2.ToString("yyyy-MM-dd"));   //格式化 2014-10-01
    View Code

    object类型

    装箱:将值类型转化为object类型
         int i=10;
         object o1=i;                  //隐式装箱
         object o2=(object)i;      //显式装箱
    
    拆箱:将obj类型转化为一个值类型
         int i=10;
         object obj=i;    //装箱
         int j=(int)obj;   //拆箱
    View Code

    var弱类型

    1. var变量必须在定义时初始化:
        var i=100;    //OK
        var s;  
        s = "abcd";     //NO,必须var s= "abcd"; 
    2. 不能给var变量赋与初始化值类型不同的值:
        var s= "abcd";     s="hello";   //OK
        s=100;   //NO
    3. var要求是局部变量。
    4. var定义变量和object不同,它在效率上和使用强类型方式定义变量完全一样。(装箱与拆箱的过程是很损耗性能的)
    View Code

    ref引用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HelloWorld
    {  
        class Program
        {
            static void Swap(ref int a, ref int b)
            {
                int t=a;
                a = b;
                b = t;
    
            }
            static void Main(string[] args)
            {
                int a = 3, b = 4;
                Console.Write("{0},{1}
    ",a,b);
                Swap(ref a,ref b); //要加ref
                Console.Write("{0},{1}
    ",a,b);
                Console.ReadKey();
            }
            
        } 
    }
    View Code

    foreach用法

    int odd = 0, even = 0;
                int[] arr = { 1, 3, 5, 8, 11 };
                foreach(int x in arr)
                if (x % 2 == 0) even++;
                else odd++;
                Console.WriteLine("{0},{1}",odd,even);
    View Code

    异常处理

    try 
    { 
          //执行的代码(可能有异常)。
        //一旦发现异常,则立即跳到catch执行。
    } 
    catch 
    { 
          //处理异常。(若try行代码没有异常,则不执行catch内代码)
    } 
    finally 
    { 
          //不管是否有异常都会执行finally,包括catch 里面用了return。
    } 
    View Code

    二维数组和交错数组

    int[,] a = new int[2, 3];
                for (int i = 0; i < 2; i++)
                    for (int j = 0; j < 3; j++) a[i,j] = i * 3 + j;
                for(int i=0;i<2;i++)
                {
                    for (int j = 0; j < 3; j++) Console.Write("{0} ",a[i,j]);
                    Console.WriteLine();
                }
                /* 交错数组 */
                int[][] jaggedArr = new int[4][]; //变长
                jaggedArr[0] = new int[] { 1, 3, 5, 7, 9, 11 };
                jaggedArr[1] = new int[] { 1, 1 };
                jaggedArr[2] = new int[] { 2, 4, 6 };
                jaggedArr[3] = new int[] { 1, 0, 0, 0, 1 };
    View Code

    ArrayList

    ArrayList arrlist1 = new ArrayList();
    //新增数据,可以是任何类型  
    arrlist1.Add(123);
    arrlist1.Add("abc");           
    //修改数据
    arrlist1[1] = 100;           
    //插入数据  
    arrlist1.Insert(1, "xyz");
    //移除数据  
    //arrlist1.RemoveAt(0);
    Console.WriteLine("元素个数=" + arrlist1.Count );
    foreach (var x in arrlist1)   Console.WriteLine(x);
    
    
    List<string> list = new List<string>();
    list.Add("Tom");    list.Add("Mary");    list.Insert(1, "Mike");
    //添加数组
    string[ ] ss = { "Jerry", "Jim", "David" };
    list.AddRange(ss);
    list.Remove("Jerry");      list.RemoveAt(0);     //删除
    list.Sort();  //排序,默认升序
    list.Reverse();  //反序
    if( ! list.Contains("Jerry"))   Console.WriteLine("Jerry已经不存在了!");
    Console.WriteLine("元素个数=" + list.Count);
    foreach (string x in list)  Console.WriteLine(x);            
    list.Clear();  //清空
    View Code
  • 相关阅读:
    ListView具有多种item布局——实现微信对话列
    CSDN 2013年度博客之星评选——分享几张厦门杭州的美图
    不做旁观者,给博主最有力的支持——博客之星评选,期待您的支持,谢谢路过的朋友投上您宝贵的一票
    Android应用中使用及实现系统“分享”接口
    android手势创建及识别
    JSON数据源的多值参数实现
    葡萄城报表本地设计器
    如何设计带查询条件的报表
    巧用SQL语句补足不完全数据报表
    表格数据分组报表
  • 原文地址:https://www.cnblogs.com/wust-ouyangli/p/6101839.html
Copyright © 2011-2022 走看看