C#编程入门_杂项_12

来源:互联网 发布:福禄克网络网线测试 编辑:程序博客网 时间:2024/06/10 09:09

21篇C#博客的配套源码


static关键字

StaticClass类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 静态类{    //静态类就是用static 修饰的类    //静态方法就是用static修饰的方法    //静态类是无法被继承的    //实例类中可以包含静态成员和实例成员    static class StaticClass    {        //静态类中不能出现实例字段        //private string name;        //静态类中能包含静态的字段        private static string name;        //静态类中不能出现实例方法        //public int Add(int num1,int num2)        //{        //    return num1 + num2;        //}        public static int number;        public static string Name        {            get            {                return name;            }            set            {                name = value;            }        }        //静态类中不能有实例构造方法        //public StaticClass()        //{        //}        //静态类中不能出现访问修饰符        //静态类虽然不能被实例化 但是静态构造方法也能被调用        //静态构造函数无访问修饰符、无参数,只有一个 static 标志。        //静态构造函数不可被直接调用,当创建类实例或引用任何静态成员之前,静态构造函数被自动执行,并且在某次运行期间只执行一次。        //静态构造函数最多只运行一次。        //静态构造函数不可以被继承。  所有的构造函数都不可以被继承        static StaticClass()        {            Console.WriteLine("***");        }        //静态类中只能包含静态的方法         public static int Add(int num1,int num2)        {            return num1 + num2;        }        public static void NumberCreate()        {            number++;        }    }}

StaticClass01类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 静态类{    class StaticClass01    {        //属于实例  调用的时候是认为调用        public StaticClass01()        {            Console.WriteLine("实例构造方法");        }        //属于类  因为调用时候是被系统调用        static StaticClass01()        {            Console.WriteLine("静态构造方法");        }        public static void Test()        {            Console.WriteLine("++++++++++++++");        }    }}

Program类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 静态类{    class Program    {        //如果类中包含用来开始执行的 Main 方法,则该类的静态构造函数将在调用 Main 方法之前执行。        //如果类中的静态字段带有初始化,则静态字段的初始化语句将在静态构造函数之前运行。        //一个类只能有一个静态构造函数。        static int num = 200;        static Program()        {            num = 12000;        }        static void Main(string[] args)        {            //静态类不能作为一种自定义的类型            //静态类不能被实例化            //StaticClass.Add(1,1);            //Console.WriteLine(num);            //Test01();            //Test02();            //通过类名.方法名调用静态方法的时候会默认的调用一次静态构造方法            StaticClass01.Test();            //一个类中既含有静态构造方法 也含有实例构造方法 在创建实例的时候 会首先调用一次静态构造方法再调用一次实例构造方法                StaticClass01 sc = new StaticClass01();        }        static void Test01()        {            StaticClass.NumberCreate();            Console.WriteLine(StaticClass.number);        }        static void Test02()        {            StaticClass.NumberCreate();            Console.WriteLine(StaticClass.number);        }    }}

索引器

索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的。

索引器和数组比较:

(1)索引器的索引值(Index)类型不受限制

(2)索引器允许重载

(3)索引器不是一个变量

索引器和属性的不同点

(1)属性以名称来标识,索引器以函数形式标识

(2)索引器可以被重载,属性不可以

(3)索引器不能声明为static,属性可以
语法格式
索引器的定义方式与属性定义方式类似,其基本的语法格式如下所示。
[修饰符] 数据类型 this[索引类型 index]
{
get{ //返回参数值}
set{ //设置隐式参数value给字段赋值}
}
在上述语法格式中,使用this关键字加“[索引类型 index]”的形式来创建一个索引器,在索引器中同样会使用get和set访问器,来获取属性值和设置属性值。

通过索引器实现一个简单的可变数组

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 索引器{    class StringClass    {        private string[] strArr;        //计数器 记录数组里面实际存放数据的数量        private static int count;        static StringClass()        {            count = 0;        }        public int Count        {            get            {                return count;            }        }        //数组的容量        public int Capicity        {            get            {                return strArr.Length;            }        }        public StringClass()        {            strArr = new string[4];        }        public string this[int index]        {            get            {                return strArr[index];            }            set            {                if (index<strArr.Length)                {                    strArr[index] = value;                }                else //数组扩容  每次扩容都增加一倍                {                    string[] arr = new string[2 * strArr.Length];                    for (int i = 0; i < strArr.Length; i++)                    {                        arr[i] = strArr[i];                    }                    strArr = arr;                    strArr[index] = value;                }                //保证数组中的存放数据的个数永远比最大索引值大1                if (count <= index)                {                    count = index + 1;                }            }        }    }}

垃圾回收


命名空间与程序集

命名空间
在实际开发过程中,除了自己编写的程序外还存在引用其他程序的情况,这时可能会碰到类名相同的情况。为此,C#中引入了命名空间这个概念,可以将命名空间理解为程序定义的一个目录,使用命名空间可以有效避免类名冲突的问题
例如
namespace Example
{
class Animal
{
void Shout()
{
Console.WriteLine(“动物的叫声”);
}
}
}
上述代码中,namespace是表示命名空间的关键字,Example表示命名空间名,在实际开发中,命名空间都是以公司名或者项目名为前缀的,例如Itcast.Example。
当程序中需要调用其他命名空间的类时,可以在程序中使用完整的限定名,例如下面这段代码,在实例化对象、调用方法和属性时都要使用“命名空间名.类名.成员”的方式来引用。
例如Example.Animal animal= new Example.Animal();

由于使用完全限定名的方式不利于程序代码的阅读,而且会导致代码的冗余,所以C#中还可以使用 using 关键字添加对命名空间的引用,这样在程序中调用其他命名空间下的类时就可以直接使用,而无需使用完整的限定名,例如下面这段代码就是使用了 using 关键字引入了Example这个命名空间。
所以我们一般在程序的最开头加上 using Example;
如果在两个命名空间中都有相同的类,在一个方法中使用的时候,那么我们必须要使用完全限定名的方式调用。
程序集
迄今为止所有开发的程序使用的都是自己的类,然而在许多项目中可能会用到其他程序中的类,此时就需要使用程序集(扩展名为.dll),所谓的程序集就是包含一个或多个类型的定义文件和资源文件的集合,该程序集中的文件可以被其他程序使用。
程序集文件可分为四个部分,分别是程序集清单、元数据、CIL、资源集,具体说明如下
● 程序集清单:包含描述该程序集中各元素彼此如何关联的数据集合,还包含指定该程序集的版本信息、安全标识所需的元数据、定义该程序集的范围以及解析对资源和类应用所需的元数据。
● 元数据:提供有关程序集中定义的类型信息,包括类型的名称、基类和类型所实现的接口等。
● CIL:程序类型中所有的中间代码。
● 资源集:诸如位图、指针、静态文本等。


const和readonly

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Const和ReadOnly{    class Test    {        //声明的时候并赋值        private readonly int age = 10;        private readonly static int num1 = 0;        private readonly int num2 = 0;        private static int num3 = 0;        //静态只读字段只能在静态构造方法中赋值        static Test()        {            num1 = 222;        }        //非静态字段只能在非静态构造方法中赋值        public Test()        {            //使用readonly修饰的的常量可以在构造方法中修改多次            age = 100;            age = 200;            //静态字段可以在非静态构造方法中赋值            num3 = 33333;            //静态只读字段无法在非静态构造方法中赋值            //num1 = 11111;        }        public void Print()        {            //在非构造方法中无法对readonly修饰的常量赋值            //age = 300;            Console.WriteLine(age);        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Const和ReadOnly{    class Program    {        static void Main(string[] args)        {            //const本身就是一个静态的            const int age = 10;            const double PI = 3.14;            Console.WriteLine(age);            Test t = new Test();            t.Print();        }    }}

const 定义的是静态常量在声明的同时赋值.以后不能改变它的值.属于编译时常量。不能用new初始化。

const一般只能修饰值类型和String
也可以修饰引用类型 但是引用类型只能为null(废物)

readonly 是只读变量.属于运行时变量.可以在类constructor里改变它的值.不能作用于局部变量。
初始化时机:
1.声明的同时赋值
2.静态变量的话 在静态构造中初始化
3.非静态变量的话 在非静态构造中初始化

readonly 关键字与 const 关键字不同。
1. const 字段只能在该字段的声明中初始化。readonly 字段可以在声明或构造函数中初始化。因此,根据所使用的构造函数,readonly 字段可能具有不同的值。
2. const 字段是编译时常数,而 readonly 字段可用于运行时常数。
3. const 默认就是静态的,而 readonly 如果设置成静态的就必须显示声明。
4.const 对于引用类型的常数,可能的值只能是 string 和 null。
readonly可以是任何类型


运算符重载

运算符可以进行基本数据类型之间的运算,想把自己定义的类型进行类似“+ - * /”之类的运算就要用到运算符重载
对于+-运算符完全可以提供对象的方法来完成
运算符重载的一般形式是
public static返回类型operator运算符(参数表){
语句;
}

class Test{        public int x1;        public int x2;        public static Test operator +(Test t1,Test t2)        {            t1.x1 = t1.x1 + t2.x1;            t1.x2 = t1.x2 + t2.x2;            return t1;        }        public static Test operator *(Test t1, Test t2)        {            t1.x1 = t1.x1 * t2.x1;            t1.x2 = t1.x2 * t2.x2;            return t1;        }        public void Show()        {            Out.WriteLine("{0}   {1}",x1,x2);        } }

结构

结构是值类型

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 结构{    class PointClass    {        private int x;        private int y;        public PointClass()        {        }        public PointClass(int x, int y)        {            this.X = x;            this.Y = y;        }        public int X        {            get            {                return x;            }            set            {                x = value;            }        }        public int Y        {            get            {                return y;            }            set            {                y = value;            }        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 结构{    struct PointStruct    {        public int x;        public int y;        //结构不能显示的包含无参构造        //public PointStruct()        //{        //}        //在结构中显示的写出有参构造  不显示的写出无参构造  无参构造可以正常使用        public PointStruct(int x, int y)        {            this.x = x;            this.y = y;        }        public void Test()        {            Console.WriteLine("***********");        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 结构{    class Program    {        static void Main(string[] args)        {            //验证结构是值类型            //PointStruct ps = new PointStruct();            //ps.x = 100;            //ps.y = 200;            //PointStruct ps1 = ps;            //ps1.x = 200;            //ps1.y = 300;            //如果ps.x 和ps.y的值保持不变  那么证明结构是值类型            //Console.WriteLine(ps.x +"  " + ps.y);            //Console.WriteLine(ps1.x+"   "+ ps1.y);            //结构在下面的过程中只有穿件数组的时候穿件了一个对象            PointStruct[] psArr = new PointStruct[10];            for (int i = 0; i < psArr.Length; i++)            {                psArr[i].x = i;                psArr[i].y = i;            }            //而类在下面的过程中创建了一共11个对象            PointClass[] pcArr = new PointClass[10];            for (int i = 0; i < pcArr.Length; i++)            {                pcArr[i] = new PointClass(100+i,200+i);            }            //对比雷和结构,我们在做简单的,没有太多逻辑的时候使用结构相对比类要节省很多内存空间        }    }}

枚举

枚举是值类型

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 枚举{    public enum Tools    {        HUIXING_YINPIN = 22201,        HUIXING_LANYA ,        LANCHUANG_YINPIN = 67601,        LANCHUANG_LANYA = 67602    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 枚举{    class Program    {        static void Main(string[] args)        {            //Test(Tools.HUIXING_YINPIN);            //Console.WriteLine((int)Tools.LANCHUANG_LANYA);            string numer = "22202";            Test((Tools)(int.Parse(numer)));        }        static void Test(Tools tool)        {            switch (tool)            {                case Tools.HUIXING_YINPIN:                    Console.WriteLine("汇兴音频刷卡器");                    break;                case Tools.HUIXING_LANYA:                    Console.WriteLine("汇兴蓝牙刷卡器");                    break;                case Tools.LANCHUANG_YINPIN:                    Console.WriteLine("蓝创音频刷卡器");                    break;                case Tools.LANCHUANG_LANYA:                    Console.WriteLine("蓝创蓝牙刷卡器");                    break;                default:                    break;            }        }    }}

原创粉丝点击