C#(三):命名空间、访问修饰符、枚举、集合、静态、只读、常量
admin
2024-04-30 18:46:20
0次

命名空间、访问修饰符、枚举、集合、静态、只读、常量

    • 命名空间
    • 访问修饰符
    • `enum`
    • 集合
      • `List`
      • `ArrayList`
      • `Sort` 排序
      • `Hashtable`
      • `Dictionary`
    • static
    • `readonly` 和 `const`

命名空间

  • 命名空间是一个虚拟的集合
  • 命名空间中只能是 类 、 结构体 、 枚举 、 接口 等类型
  • 命名空间防止类名重复,更好的管理类
using System;
using AAA;namespace ConsoleAppDemo
{class MainClass{public static void Main(string[] args){Person.name = "Lee"; // using AAA; 默认会去使用AAA中的PersonBBB.Person.name = "Zhang";Animal.name = "Dog"; // using AAA; 默认会去使用AAA中的Animal}}}namespace AAA {static class Person{public static string name;}static class Animal{public static string name;}
}namespace BBB
{static class Person{public static string name;}namespace CCC{static class Person{public static string name;}}
}

访问修饰符

注意:权限低的访问不了权限高的,权限低的也当不了权限高的的父类

访问修饰符描述
private成员只能在类型的内部访问,这是默认设置
internal成员可在类型的内部或同一程序集的任何类型中访问
protected成员可在类型的内部或从类型继承的任何类型中访问
public成员在任何地方都可以访问
internal protected成员可在类型的内部、同一程序集的任何类型以及从该类型继承的任何类型中访问,与虚构的访问修饰符 internal_or_protected 等效
private protected成员可在类型的内部、同一程序集的任何类型以及从该类型继承的任何类型中访问,相当于虚构的访问修饰符 internal_and_protected。这种组合只能在C#7.2或更高版本中使用

enum

// 枚举中指定元素类型(并且只能配置成整型)及默认值
enum Weekday : int
{Sun = 0, Mon, Tue, Wed, Thu, Fri, Sat,
}internal class Program
{static void Main(string[] args){Console.WriteLine(Weekday.Sun);             // SunConsole.WriteLine(Weekday.Sun == 0);        // TrueConsole.WriteLine((int)Weekday.Sun == 0);   // TrueConsole.WriteLine(Weekday.Mon);             // Mon//Console.WriteLine(Weekday.Mon == 1);      // ErrorConsole.WriteLine((int)Weekday.Mon == 1);   // TrueConsole.WriteLine(Weekday.Tue);             // TueConsole.WriteLine((int)Weekday.Tue);        // 2Console.WriteLine(Weekday.Wed);             // WedConsole.WriteLine(Weekday.Thu);             // ThuConsole.WriteLine(Weekday.Fri);             // FriConsole.WriteLine(Weekday.Sat);             // SatWeekday weekday = Weekday.Sun;// 周日switch (weekday){case Weekday.Sun:Console.WriteLine("周日");break;case Weekday.Mon:Console.WriteLine("周一");break;case Weekday.Tue:Console.WriteLine("周二");break;case Weekday.Wed:Console.WriteLine("周三");break;case Weekday.Thu:Console.WriteLine("周四");break;case Weekday.Fri:Console.WriteLine("周五");break;case Weekday.Sat:Console.WriteLine("周六");break;}}
}

集合

class Person
{public string name;public int age;public Person(string name, int age){this.name = name;this.age = age;}
}

List

using System.Collections.Generic;internal class Program
{static void Main(string[] args){// List persons = new List();// persons.Add(new Person("Lee", 25));// persons.Add(new Person("ZhangSan", 30));List persons = new List{new Person("Lee", 25),new Person("ZhangSan", 30)};// List persons = new List// {//     new Person("Lee", 25),//     123,//     "abc"// };foreach (var person in persons){Console.WriteLine(person.name);Console.WriteLine(person.age);}}
}
 

ArrayList

using System.Collections;internal class Program
{static void Main(string[] args){// ArrayList personList = new ArrayList();// personList.Add(new Person("Lee", 25));// personList.Add(123);// personList.Add("abc");ArrayList personList = new ArrayList{new Person("Lee", 25),123,"abc"};foreach (var person in personList){Console.WriteLine(person); // ConsoleApp.Person 123 abc}}
}

Sort 排序

internal class Program
{static void Main(string[] args){ArrayList personList1 = new ArrayList();personList1.AddRange(new Person[] {new Person("001", "Lee", 25),new Person("002", "张三", 10),new Person("003", "李四", 20),new Person("004", "王五", 20),new Person("005", "赵六", 18),});personList1.Sort();foreach (Person person in personList1){//[001] Lee: 25//[003] 李四: 20//[004] 王五: 20//[005] 赵六: 18//[002] 张三: 10Console.WriteLine($"[{person.id}] {person.name}:{person.age}");}List personList2 = new List();personList2.AddRange(new Person[] {new Person("001", "Lee", 25),new Person("002", "张三", 10),new Person("003", "李四", 20),new Person("004", "王五", 20),new Person("005", "赵六", 18),});personList2.Sort();foreach (Person person in personList2){//[001] Lee: 25//[003] 李四: 20//[004] 王五: 20//[005] 赵六: 18//[002] 张三: 10Console.WriteLine($"[{person.id}] {person.name}:{person.age}");}}
}class Person : IComparable
{public string id;public string name;public int age;public Person(string id, string name, int age){this.id = id;this.name = name;this.age = age;}public int CompareTo(object obj){// 判断obj是不是Personif (obj is Person){// 将obj转型为Person类型Person person = obj as Person;//return age - person.age; // 升序return person.age - age; // 降序}return 0; // 默认}}

Hashtable

using System.Collections;Hashtable obj = new Hashtable();
obj.Add("name", "Lee");
obj.Add("age", 18);
obj.Add(6, true);//Hashtable obj = new Hashtable
//{
//    { "name", "Lee" },
//    { "age", 18 },
//    { 6, true }
//};ICollection keys = obj.Keys;
ICollection values = obj.Values;foreach (object key in keys)
{Console.WriteLine(key); // 6 age name
}foreach (object value in values)
{Console.WriteLine(value); // True 18 Lee
}foreach (DictionaryEntry item in obj)
{Console.WriteLine($"obj[{item.Key}]={item.Value}"); // obj[6]=True obj[age] = 18 obj[name] = Lee
}

Dictionary

using System.Collections.Generic;Dictionary dict = new Dictionary
{{ "name", "Lee" },{ "age", 18 },{ "sex", "男" }
};foreach (KeyValuePair item in dict)
{Console.WriteLine($"dict[{item.Key}]={item.Value}"); // dict[name]=Lee dict[age]=18 dict[sex]=男
}

static

  • 静态的属性是属于类的需要使用类进行访问
  • 非静态的属性是属于对象的需要实例化对象进行访问
    class MainClass
    {public static void Main(string[] args){// 解决打印中文乱码问题Console.OutputEncoding = System.Text.Encoding.UTF8;Person.Forefathers = "类人猿"; // 静态属性归类进行访问Person lee = new Person();lee.name = "ProsperLee"; // 非静态属性归对象进行访问lee.Info(); // 姓名:ProsperLeePerson zhang = new Person {name = "张三",};zhang.Info(); // 姓名:张三lee.ForefathersInfo(); // ProsperLee 的祖先是 类人猿zhang.ForefathersInfo(); // 张三 的祖先是 类人猿}}class Person
    {public static string Forefathers;public string name;public void Info(){Console.WriteLine($"姓名:{name}");}public void ForefathersInfo() {Console.WriteLine($"{name} 的祖先是 {Forefathers}");}}
    

静态类

  • 静态类不能实例化对象 static Xxx{} -> new Xxx() -> Error
  • 静态类中不允许写非静态的成员 static Xxx{ public void Fun(){} } -> Error
  • 静态类只能有一个父类 Object 不能继承也不能被继承

示例

class MainClass
{public static void Main(string[] args){Person.name = "Lee";Console.WriteLine(Person.name); // Person Show ~ LeeConsole.WriteLine(Person.name); // Lee}
}public static class Person {public static string name;static Person() {Console.WriteLine("Person Show ~");}}

用途

  • 一般用来设计工具类(只需要去调用类中的方法去实现功能,没必要实例化的类)

readonly 和 const

  • const 常量必须拥有初始值, readonly 可以没有
  • readonly 可以在构造方法中进行赋值修改, const 不可以
class Person
{public readonly int x;// public const int y; // Errorpublic const int y = 100;public Person() {x = 200;// y = 200; // Error}}

相关内容

热门资讯

蜻蜓订购APP贵金属订购非法期...   蜻蜓订购APP以"投资平台"名义对外宣传,主打贵金属期货(订购)交易。该平台一方面强调自身"正规...
悟空订购APP非法期货交易欺骗...   近期,有投资者反映,“悟空订购APP”等平台以白银、贵金属订购为名,宣传所谓“高收益”“低门槛”...
小赢淘金APP期货投资骗局,平...   “小赢淘金”之流,最擅长的就是用“注册送现金”、“充值返券”的糖衣炮弹来引流。别天真地以为那是福...
八方淘金APP非法期货交易欺骗...   警惕!八方生财、每日淘金、小赢淘金、悟空订购、蜻蜓订购、斗金订购等APP均涉嫌非法期货交易!  ...
智易购APP贵金属订购交易骗局...   很多人看到“3元起投”“充值送福利”“操作简单”“随时提现”这类宣传,很容易把智易购APP当成普...
喜玛银庄APP商品订购投资骗局...   喜玛银庄APP白银、铂金“现货订购”骗局警示:无资质平台涉嫌非法期货交易,投资者爆仓后如何维权?...
吉鑫乐享APP炒贵金属订购交易...   近期,多名投资者反映在“吉鑫乐享APP”进行贵金属投资时遭遇严重亏损,甚至血本无归。看似正规的“...
易掘金APP虚假期货投资骗局,...   “易掘金”的APP它打着“高收益、灵活投资”的旗号,宣称通过“商品订购”就能轻松赚钱,吸引了不少...
在大吉订购APP订购交易亏损被...   近期,不少投资者反映在“大吉订购App”上投资白银、铂金等商品遭遇严重亏损。该平台宣称提供现货订...
铂银壹号APP商品订购交易亏损...   许多中小投资者误以为网上的“商品现货订购”是合规合法的理财渠道,殊不知这背后隐藏着巨大的合规风险...
兴鑫淘金APP贵金属订购交易亏...   白银、贵金属或其他商品类投资,除国家批准的正规期货交易场所及持牌期货公司外,市面上大量第三方交易...
铂银掘金APP无资质的违规平台...   铂银掘金等平台的投资者,往往是被“注册送现金”“充值返现券”“首单免亏”等广告话术吸引,进而下载...
八方淘金APP订购交易不合规,...   “八方淘金APP”“铂银掘金APP”“铂银壹号APP”等软件被指以“现货订购”为名,实际采用高杠...
在指尖订购APP订购商品亏损被...   指尖订购APP这类平台,表面是“订购交易”,实则是披着电商外衣的非法期货陷阱。绝大多数投资者被蒙...
在犀贝易购APP做商品订购交易...   市面上打着“现货投资”“贵金属交易”旗号的APP并不少,但真正合规、受监管的平台并不多。犀贝易购...
易达淘金APP虚假商品订购交易...   易达淘金APP存在严重的虚假宣传与违规交易行为。该平台打着“商品订购”的旗号,实则通过不合规的交...
大浪淘金APP白银订购亏损被骗...   大浪淘金APP最隐蔽的套路,在于其完全封闭的内盘交易系统。表面上,它宣称与国际贵金属市场接轨;实...
一品好购APP上订购白银出现亏...   一品好购APP原名“一品订购”,运营方为上海宝智未来信息技术有限公司。近期不少投资者反映在该平台...
九州订购APP贵金属订购投资骗...   九州订购APP正通过抖.音、快手、今日头条等平台大量投放投资广告,用“5元起投”“充值返现”“0...
坚锅淘金APP交易软件并不正规...   当前,市面上大量所谓的“现货商城订购”交易软件,早已偏离了正常的实物商品买卖,演变为高杠杆、高手...