原创 读书笔记

2011-2-26 19:30 1217 6 6 分类: 工程师职场

新手学c#

第一章
微软VS编辑器主要有
Visual Studio 2003对应.NET Framework1.1,C#1.2
Visual Studio 2005对应.NET Framework2.0,C#2.0
Visual Studio 2008对应.NET Framework3.5,C#3.0

1APS.NET
this.Form.InnerText = "这是测试页";//设定加载后的页面显示的文字
2FORM
this.Text = "hello world";//设置窗体名称
3CONSOLE
Console.Write("hello world");//输出相关信息
Console.Read(); //等待用户输入

第二章
1

        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++) //在循环中声明一个变量i;
            {
                Console.WriteLine(i);
            }    //i的作用域结束

            for (int i = 10; i < 100; i++) //在这个循环中重新声明变量i
            {
                Console.WriteLine(i);
            }    //i的作用域结束
        }

2
        static void Main(string[] args)
        {
            string s1 = "a string";    //声明一个string型变量,并赋初值
            string s2 = s1;     //声明一个string型变量,并将s1的值赋给s2
            Console.WriteLine("s1 i" + s1); //调用系统自定义的输出方法输出s1的值,结果“a string”
            Console.WriteLine("s2 is" + s2); //调用系统自定义的输出方法输出s2的值,结果”a string”
            s1 = "new string";    //给s1赋新值
            Console.WriteLine("s1 now value is" + s1); //调用系统自定义的输出方法输出s1的值,结果”new string”
            Console.WriteLine("s2 now value is" + s2); //调用系统自定义的输出方法输出s2的值,结果”a string”
            Console.Read();
        }

3
        static void Main(string[] args)
        {
            byte b = 255; //声明一个byte型变量,并赋初值为255
            checked  //声明代码块需要进行溢出检测
            {
                b++;  //抛出异常,由于byte型变量最大为255,所以当再次加1时抛出异常
            }

            byte c = 255; //声明一个byte型变量,并赋初值为255
            unchecked //声明代码块不需要进行溢出检测
            {
                c++;  //不抛出异常,但由于byte型变量最大为255,所以导致c数据丢失,c变为0
            }
          
4
        static void Main(string[] args)
        {
            int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 }; //声明一个整数数组
            foreach (int i in numbers)    //遍历数组中的每个元素
            {
                Console.WriteLine(i);    //输出每个元素
                // i++;     //错误
            }


            for (int i = 0; i < 9; i++)     //遍历数组中的每个元素
            {
                Console.WriteLine(i);    //输出每个元素
                numbers++;   //将集合的值加1
            }
        }

5
    class Program
    {
        static void Main(string[] args)
        {
            //声明一个枚举变量
            weekDay wd = weekDay.Sunday;
            //比较枚举值,并根据枚举值输出信息
            switch (wd)
            {
                case weekDay.Monday:    //如果是星期一
                    Console.WriteLine("this is monday"); //输出信息
                    break;
                case weekDay.Thursday:     //如果是星期二
                    Console.WriteLine("this is Thursday"); //输出信息
                    break;
                case weekDay.Wednesday:     //如果是星期三
                    Console.WriteLine("this is Wednesday"); //输出信息
                    break;
                case weekDay.Tuesday:      //如果是星期四
                    Console.WriteLine("this is Tuesday");  //输出信息
                    break;
                case weekDay.Friday:      //如果是星期五
                    Console.WriteLine("this is Friday");  //输出信息
                    break;
                case weekDay.Saturday:      //如果是星期六
                    Console.WriteLine("this is Saturday");  //输出信息
                    break;
                case weekDay.Sunday:      //如果是星期天
                    Console.WriteLine("this is Sunday");  //输出信息
                    break;
                default:        //如果不是一个星期中的任何一天
                    Console.WriteLine("no this day");   //输出信息
                    break;
            }
        }

        enum weekDay
        {
            Monday = 1,       //枚举中1对应的字符
            Tuesday = 2,       //枚举中2对应的字符
            Wednesday = 3,       //枚举中3对应的字符
            Thursday = 4,       //枚举中4对应的字符
            Friday = 5,        //枚举中5对应的字符
            Saturday = 6,       //枚举中6对应的字符
            Sunday = 7       //枚举中7对应的字符
        }
    }

第三章

1
    class Program
    {
        static void Main(string[] args)
        {
            InitialExample fe = new InitialExample();     //实例化示范类
            Console.ReadLine();        //等待用户输入
        }

        class showInfo          //声明显示信息的类
        {
            //类的构造函数,要求传入参数字符串
            public showInfo(string info)
            {
                Console.WriteLine(info);      //输出信息
            }
        }

        class InitialExample //声明一个示范类
        {
            public static showInfo ssi = new showInfo("静态成员"); //静态成员
            public showInfo si = new showInfo("实例成员");   //实例成员
            //构造函数
            public InitialExample()
            {
                Console.WriteLine("构造函数");     //输出信息
            }
        }
    }

2
    class Program
    {
        //声明一个操作类,专门用来改变参数值
        public class operateClass
        {
            //改变参数值的方法
            public void changeValue(string waitValue)
            {
                waitValue = "changed";    //初始化变量
                Console.WriteLine(waitValue);   //输出变量
            }
        }
        //声明一个拥有测试的类,里面有一个公共的字段
        public class testEx
        {
            public string nameS; //公共字段
        }
        //主函数
        static void Main(string[] args)
        {
            string aimValue = "initial";         //声明一个字符串变量
            testEx te = new testEx();         //声明一个对象,并且给对象赋值
            te.nameS = "another initial";
            operateClass oc = new operateClass();      //声明一个转换类的对象
            Console.WriteLine(aimValue);    //输出两个值
            Console.WriteLine(te.nameS);
            oc.changeValue(te.nameS);         //调用转换方法转换两个值
            oc.changeValue(aimValue);
            Console.WriteLine(te.nameS);        //输出转换过之后的值
            Console.WriteLine(aimValue);
            Console.ReadLine();          //等待用户输入值
        }
    }

3
    class Program
    {
        static void Main(string[] args)
        {
            GiveValue tempGV = new GiveValue(); //实例化类
            tempGV.typeofname = "ming";   //给对象的typeofname字段赋值,并输出结果
            Console.WriteLine(tempGV.typeofname);
            tempGV.SetName("jie");    //设置私有字段的值,并读取字段输出
            Console.WriteLine(tempGV.GetName());
        }
        class GiveValue
        {
            public string typeofname;     //类型名,公有的
            private string name;        //名称,私有,不能直接访问
            //设置私有字段name值
            public void SetName(string newname)
            {
                name = newname;    //设置字段明
            }
            //得到私有字段name值
            public string GetName()
            {
                return name;     //返回值
            }
        }
    }


4
    class Program
    {
        static void Main(string[] args)
        {
            //直接调用示例类的静态方法
            Console.WriteLine(staticOperate.AddNumber(1, 2));     //结果为3
            Console.WriteLine(staticOperate.devideNumber(4, 2));    //结果为2
            Console.WriteLine(staticOperate.mutiplyNumber(3, 4));   //结果为12
            Console.WriteLine(staticOperate.plusNumber(5, 2));   //结果为3
            //实例化类,并调用实例化方法
            staticOperate so = new staticOperate();
            so.insNumer(10);           //输出方法
            Console.ReadLine();
        }
        //静态类
        public class staticOperate
        {
            //静态加方法
            public static int AddNumber(int num1, int num2)
            {
                return num1 + num2;        //返回相加结果
            }
            //静态乘方法
            public static int mutiplyNumber(int num1, int num2)
            {
                return num1 * num2;        //返回相乘结果
            }
            //静态除方法
            public static int devideNumber(int num1, int num2)
            {
                return num1 / num2;       //返回相除结果
            }
            //静态减方法
            public static int plusNumber(int num1, int num2)
            {
                return num1 - num2;       //返回相减结果
            }
            //实例化输出方法
            public void insNumer(int num)
            {
                Console.WriteLine(num); //输出指定值
            }
        }
    }

5

    class Program
    {
        static void Main(string[] args)
        {
            operateClass oc = new operateClass();  //实例化一个操作类
            float f1 = 1.234f;          //声明参数
            float f2 = 2.334f;          //声明参数
            int num1 = 12;          //声明参数
            int num2 = 32;          //声明参数
            //调用操作类的相加方法,并输出计算结果
            Console.WriteLine(oc.AddThing(f1, f2));   //结果,浮点相加,3.568
            Console.WriteLine(oc.AddThing(num1, num2)); //结果,整数相加,44
            Console.WriteLine(oc.AddThing("122", "221"));  //结果,字符串相加,122221

            Console.ReadLine();
        }
        //声明一个操作类
        class operateClass
        {
            //浮点数相加
            public float AddThing(float f1, float f2)
            {
                Console.WriteLine("浮点相加"); //输出操作类型
                return f1 + f2;    //返回操作结果
            }
            //整数相加
            public int AddThing(int num1, int num2)
            {
                Console.WriteLine("整数相加"); //输出操作类型
                return num1 + num2;   //返回操作结果
            }
            //字符串相加
            public string AddThing(string num1, string num2)
            {
                Console.WriteLine("字符串相加");//输出操作类型
                return (num1 + num2);   //返回操作结果
            }
        }


6
    class Program
    {
        static void Main(string[] args)
        {
            //实例化两个复述类
            ComplexNumber cn1 = new ComplexNumber(12, 22);     //实例化类
            ComplexNumber cn2 = new ComplexNumber(22, 32);     //实例化类
            ComplexNumber cn3 = cn1 + cn2;             //通过相加得到新的复述类
            //输出新的复述类的值
            Console.WriteLine(cn3.GetNowValue());        //结果,+54i
            Console.ReadLine();
        }
        //复数类
        public class ComplexNumber
        {
            //实部与虚部
            private int real;
            private int imaginary;
            //无参数的构造函数
            public ComplexNumber()
            {
                real = 0;             //初始化实部值
                imaginary = 0;            //初始化虚步值
            }
            //两个参数的构造函数
            public ComplexNumber(int r, int i)
            {
                real = r;             //设置实部值
                imaginary = i;            //设置虚步值
            }
            //得到当前复述的值
            public string GetNowValue()
            {
                return real.ToString() + "+" + imaginary.ToString() + "i";    //返回当前值
            }
            //重载加运算符
            public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b)
            {
                return new ComplexNumber(a.real + b.real, a.imaginary + b.imaginary); //返回结果
            }
            //重载减运算符
            public static ComplexNumber operator -(ComplexNumber a, ComplexNumber b)
            {
                return new ComplexNumber(a.real - b.real, a.imaginary - b.imaginary); //返回结果
            }
        }
    }

7

    class Program
    {
        static void Main(string[] args)
        {
            propertyClass pc = new propertyClass(); //实例化属性类
            pc.TempString = "set proper";   //给属性tempstring赋值
            //pc.GetPro = "set wronge";    错误,不能设置可读属性
            //读取属性值并输出
            Console.WriteLine(pc.TempString);  //获取属性值
            Console.WriteLine(pc.GetPro);   //获取属性值
            Console.ReadLine();
        }
        class propertyClass
        {
            //私有字段
            private string tempString;
            //能够设置和读取的属性
            public string TempString
            {
                get { return tempString; }   //读取字段值
                set { tempString = value; }   //设置字段值
            }
            private string getPro = "just Get";  //声明私有字段并设置初始值
            //设置一个仅能获取的属性
            public string GetPro
            {
                get { return getPro; }    //获取字段值
            }
        }

    }

8
    class Program
    {
        static void Main(string[] args)
        {
            propertyClass pc = new propertyClass(); //实例化属性类
            pc.TempString = "set proper";   //给属性tempstring赋值
            //pc.GetPro = "set wronge";    错误,不能设置可读属性
            //读取属性值并输出
            Console.WriteLine(pc.TempString);  //获取属性值
            Console.WriteLine(pc.GetPro);
            Console.ReadLine();
        }
        public struct propertyClass
        {
            private string tempString;    //私有字段
            //能够设置和读取的属性
            public string TempString
            {
                get { return tempString; }   //获取字段值
                set { tempString = value; }   //设置字符按值
            }
            private string getPro;  //声明私有字段,不能设置初始值
            //设置一个仅能获取的属性
            public string GetPro
            {
                get
                {
                    getPro = "just read";   //设置字符值
                    return getPro;     //返回字段值
                }
            }

9
    class Program
    {
        static void Main(string[] args)
        {
            //声明结构
            switchExample1 se1;
            switchExample2 se2;
            se1 = 10;          //给结构赋值,调用了结构switchExample1的参数为整数的隐式转换
            se2 = (switchExample2)se1;    //转换结构,调用了结构switchExample2的参数为switchExample1的显示转换的函数
            se2 = (switchExample2)(int)se1;   //转换结构,调用结构switchExample1的参数为int型和switchExample2型的显示转换函数
            se1 = se2;         //转换结构,调用结构switchExample1参数switchExample2型的隐式转换
            Console.WriteLine((int)se1);      //转换结构,调用结构switchExample1的参数为int型隐式的转换函数
            Console.WriteLine(se1);       //转换结构,调用结构switchExample2的参数为switchExample2型的隐式转换函数
            Console.ReadLine();
        }
        struct switchExample1
        {
            private int value;
            //构造函数
            public switchExample1(int value)
            {
                this.value = value;   //设置值
            }
            //隐式转换函数,转换为switchExample1参数为int型
            static public implicit operator switchExample1(int value)
            {
                return new switchExample1(value); //返回隐式的转换
            }
            //隐式转换函数,转换为switchExample1,参数为switchExample2
            static public implicit operator switchExample1(switchExample2 binary)
            {
                return new switchExample1((int)binary); //返回隐式的转换
            }
            //显示转换函数,转换为int,参数为switchExample1
            static public explicit operator int(switchExample1 roman)
            {
                return roman.value;     //返回显示的转换
            }
            //显示转换函数,转换为string,参数为switchExample1
            static public implicit operator string(switchExample1 roman)
            {
                return ("不能转换为字符串");   //返回转换
            }
        }
        struct switchExample2
        {
            private int value;
            //构造函数
            public switchExample2(int value)
            {
                this.value = value;
            }
            //隐式转换函数,转换为switchExample2参数为int型
            static public implicit operator switchExample2(int value)
            {
                return new switchExample2(value);  //返回隐式的转换
            }
            //显示转换函数,转换为switchExample2,参数为switchExample1
            static public explicit operator switchExample2(switchExample1 se)
            {
                return new switchExample2((int)se);  //返回显示的转换
            }
            //显示转换函数,转换为int,参数为switchExample2
            static public explicit operator int(switchExample2 binary)
            {
                return (binary.value);     //返回显示的转换
            }
            //显示转换函数,转换为string,参数为switchExample2
            static public implicit operator string(switchExample2 binary)
            {
                return ("不能转换为字符串");    //返回显示的转换
            }
        }
    }

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /6 下一条