zoukankan      html  css  js  c++  java
  • C#100例之1~10

    1)从"Hello,world!"开始
    using System;   //声明引用的名称空间

    class Program   //C#的Main方法必须在一个类之中
    {
        
    static void Main(string[] args)     //Main必须是一个静态的方法,它是默认的入口点
        {
            
    //这里的Console是System命名空间下的类,它表示了控制台类
            
    //利用控制台类Console的静态方法WriteLine输出
            Console.WriteLine("Hello,World!");
            Console.Read();         
    //获取输入,一般用于屏幕暂停,便于查看输出的结果
        }
    }
    2)"is"操作符
    using System;

    class App
    {
        
    public static void Main()
        {
            
    int a;                      //定义了一个变量a,它是int类型
            Console.WriteLine(a is int);        //判断a是否是int类型
            Console.WriteLine(a is long);       //判断a是否是long类型
            if (a is int)               //判断a是否int类型
            {
                Console.WriteLine(
    "a is int type!");
            }
            
    else
            {
                Console.WriteLine(
    "a is not int type!");
            }
            Console.Read();
        }
    }
    //is操作符是.net下的特殊操作符,它是利用.net平台的反射功能实现的,如C/C++就没有支持此类操作符.
    3)枚举
    using System;

    //自定义一个枚举类型
    enum WeekDay
    {
        Sunday 
    = 0, Monday, Tuesday, Wendnesday, Thursday, Friday, Saturday
    }
    class App
    {
        
    public static void Main()
        {
            WeekDay w;
            w 
    = WeekDay.Sunday;
            Console.WriteLine(w);           
    //显示枚举类型的字符串表示
            Console.WriteLine((int)w);      //显示枚举类型的数字表示
            w = WeekDay.Tuesday;
            Console.WriteLine(w);
            Console.WriteLine((
    int)w);

            Console.Read();
        }
    }
    //枚举类型的意义在于消除"魔术数字"带来的麻烦.
    4)类的静态成员--静态成员属于类的所有对象,非静态成员属于类的特定对象.
    using System;

    class A
    {
        
    public static int x = 100;      //静态成员
        public int y;                   //非静态成员
    }
    class App
    {
        
    static void Main(string[] args)
        {
            A a1 
    = new A();
            A a2 
    = new A();
            
    //对类A的两个特定对象a1和a2来说,他们有一个共同的实例成员--x(即类的静态成员),而y则是他们自己的不同实例成员.
            a1.y = 1000;
            a2.y 
    = 2000;
            
    //a1.x = 1212;      //这样访问是错误的.
            A.x = 1111;         //类的静态成员只能由类来亲自访问.
            Console.WriteLine("a1.x=" + A.x + ";a1.y=" + a1.y);
            Console.WriteLine(
    "a2.x=" + A.x + ";a2.y=" + a2.y);
            Console.Read();
        }
    }
    5)结构类型与类类型的区别
    using System;
    //结构对象是在栈分配的,传的是值(即值类型);类对象是在堆上分配的,传递的是对象内存的引用(即引用类型).
    struct A                //结构类型A
    {
        
    public int x;
        
    public void show()
        {
            Console.WriteLine(
    this.x);
        }

    }
    class B                 //类类型B
    {
        
    public int x;
        
    public void show()
        {
            Console.WriteLine(
    this.x);
        }

    }

    class App
    {
        
    //传递结构类型是直接按值复制过去的(即新建了对象,新建对象的内容的改变不会影响传递的对象)
        public static void changeStruct(A p)
        {
            p.x 
    = 101;
        }
        
    public static void changeClass(B p) //传递类类型是传递引用,即传递的对象与当前方法内的参数对象是同一对象.
        {
            p.x 
    = 202;
        }
        
    public static void Main(string[] args)
        {
            A a;            
    //结构类型对象是在栈上分配内存的,所以不需要new关键字创建.
            B b = new B();  //类类型对象一定需要new关键字在堆上分配内存.
            a.x = 100;
            b.x 
    = 200;
            a.show();
            b.show();
            
    //结构和类够调用change方法后,结构传递的是值,而类传递的是引用
            changeStruct(a);
            changeClass(b);
            a.show();
            b.show();
            
    //可以看到,结构A的对象a没有改变,而类B的对象b改变了.这正与结构的对象是栈分配而类对象是堆分配的定论符合.
            Console.Read();
        }
    }
    6)方法的重载
    using System;

    class A
    {
        
    //类A有好几个同名Max的方法,每个max方法的意义类似,但是实现细节不同.这就是重载的本质.
        public static int Max(int x, int y)
        {
            
    return (x > y ? x : y);
        }
        
    //以第一个方法为准,现在这个方法参数个数相同,但是参数类型不同
        public static double Max(double x, double y)
        {
            
    return (x > y ? x : y);
        }
        
    //参数类型相同,个数不同
        public static int Max(int x, int y, int z)
        {
            
    if (x < y) x = y;
            
    if (x < z) x = z;
            
    return x;
        }
    }
    class App
    {
        
    public static void Main()
        {
            
    int a = 2, b = 15;
            Console.WriteLine(A.Max(a, b));
            
    double c = 6.23, d = 4.14;
            Console.WriteLine(A.Max(c, d));
            Console.WriteLine(A.Max(a,b,
    22));
            Console.Read();
        }
    }
    //重载是OOP中一个相当重要的思想,重载的形式有很多,但是基本的意义是一样的.
    7)foreach:遍历对象.
    using System;

    class App
    {
        
    public static void Main()
        {
            
    int[] a = new int[5];
            
    for (int i = 0; i < 5; i++//这是传统的for语句.
            {
                a[i] 
    = i * i;
            }
            
    foreach (int x in a)        //遍历数组a输出所有元素成员.
            {
                Console.WriteLine(x);
            }
            Console.Read();
        }
    }
    8)C#的数组是自动可动态分配调整内存的对象(C#的数组就是对象)
    using System;

    class App
    {
        
    public static void Main()
        {
            
    string[] a = new string[2];     //一维string数组       
            a[0= "11111";
            a[
    1= "22222111";
            
    foreach (string s in a)
            {
                Console.WriteLine(s);
            }
            a 
    = new string[10];         //对同一数组对象,可以进行重新分配

            
    int[,] b = new int[23];   //二维数组对象

            
    int[][] c = new int[2][];     //多维数组(可将此数组进化成锯齿状数组)
            c[0= new int[10];
            c[
    1= new int[5];

            Console.Read();
        }
    }
    9)只读 readonly关键字
    using System;
    class A
    {
        
    //readonly标记的成员只能在构造器初始化时被修改
        
    //使用readonly修饰的属性一般是常识性的固定不变的数字.
        public static readonly int x = 100;
        
    public static int y = 200;
        
    static A()
        {
            x 
    = 1212;
            y 
    = 1001;
        }
    }
    class App
    {
        
    public static void Main()
        {
            Console.WriteLine(
    "x = {0},y = {1}", A.x, A.y); //
            
    //A.x = 1001;//类A的x成员是只读成员,所以不能被改变,这样写会报错
            A.y = 2001;
            Console.WriteLine(
    "x = {0},y = {1}", A.x, A.y); //
            Console.Read();
        }
    }
    10)引用型:ref;输出型:out--都是获得多返回值的手段,ref传递参数前需要初始化,out不需要
    using System;

    class App
    {
        
    static void refTest(ref int a, ref int b, ref int c)
        {
            a 
    = 10; b = 11; c = 12;
        }
        
    static void outTest(out int a, out int b, out int c)
        {
            a 
    = 100; b = 110; c = 120;
        }
        
    public static void Main()
        {
            
    int x, y, z;

            
    // refTest(ref x, ref y, ref z);//在没有初始化之前,不能使用ref关键字标志的方法
            x = 1; y = 2; z = 3;
            Console.WriteLine(
    "x = {0},y = {1},z = {2}", x, y, z);
            
    //我们来使用ref来获得返回多个值
            refTest(ref x, ref y, ref z);//同时改变x,y,z的值,即可以认为是返回多个值
            Console.WriteLine("x = {0},y = {1},z = {2}", x, y, z);

            
    int x1, y1, z1;
            outTest(
    out x1, out y1, out z1);//使用out关键字同样可以返回多个值,但是对传递的参数可以先不进行初始化.
            Console.WriteLine("x1 = {0},y1 = {1},z1 = {2}", x1, y1, z1);
            Console.Read();
        }
    }
  • 相关阅读:
    JavaScript数组升降序排列、最大值、最小值等
    css3箭头
    隐藏显示
    最后一个 last-of-type
    jquery函数封装
    为什么要使用rem
    Git的使用--如何将本地项目上传到Github
    jQuery判断是否选中
    数组索引赋值
    HTML中input和button设置同样高度却不能等高的原因
  • 原文地址:https://www.cnblogs.com/wppt/p/562223.html
Copyright © 2011-2022 走看看