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 ~");}}

用途

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

readonlyconst

  • 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}}

相关内容

热门资讯

伊朗宣布关闭霍尔木兹海峡部分区... 来源:21世纪经济报道 △伊朗伊斯兰革命卫队海军军演的主要阶段在霍尔木兹海峡展开 据伊朗方面17日...
新春守护不打烊 成都市场监管全... 春节假期开启,节日市场迎来消费高峰。近日,成都市各区(市)县市场监管局坚守监管一线,紧盯食品安全、价...
澳新银行:伦敦矿业股下跌,受价... 受金属价格下滑影响,伦敦矿业股早盘下跌。澳新 银行分析师写道,因美元走强打压了市场情绪,金价未能守住...
报告:凯德投资的私募基金业务可... 来源:滚动播报 大华继显的Adrian Loh在一份报告中称,凯德投资(CapitaLand Inv...
2025年AI落地进行时:企业... 今天分享的是:2025年AI落地进行时:企业业务、组织与人才升级实战案例集 报告共计:141页 20...
原创 中... 正当美国总统特朗普鼓吹“一日一赢”的神话时,一场美元霸权崩塌的前奏正在悄然演奏。截至今年1月份,美元...
俄罗斯石油神话正在崩塌:1.5... 王爷说财经讯:你敢信吗?2026年2月17日最新消息,足足1.5亿桶原油正漂在全球各大洋的油轮上,核...
景顺长城基金总经理康乐:主动有... 日月其迈,时盛岁新。农历马年来临,谨代表景顺长城基金,向广大投资者及所有支持和信任我们的朋友,致以最...
石家庄斜视怎么治疗?专业眼科医... 斜视带来的困扰 斜视不仅影响外观,还可能导致视力下降、立体视觉缺失等问题。比如孩子在学校看黑板、读书...
五天被三次约谈,高德地图怎么了... 来源:市场资讯 (来源:财闻) 借贷业务、火车票销售业务及网约车业务是高德被约谈的主要业务。 当前正...
金银价格大跳水 当地时间17日,全球多个主要市场因传统节日休市,贵金属市场交投清淡。 在美联储降息预期有所降温的背景...
消息称月之暗面新一轮超7亿美元... 2 月 17 日消息,据“科创板日报”报道,在完成上一轮 5 亿美元(现汇率约合 34.58 亿元人...
原创 春... 2026年春晚的钟声还未敲响,一场由机器人引发的消费飓风已提前登陆。当晚八点零七分,当《机械霓裳》节...
金价、银价,突然大跌! 继昨日下跌后,2月17日早上,现货金银再次双双急跌,现货黄金一度失守4980美元/盎司,日内跌超0....
中医特色诊疗显奇效 郴州市苏仙... 大众卫生报·新湖南客户端2月10日讯(通讯员 黄伟芳 邓丽梅)近日,郴州市苏仙区南塔街道社区卫生服务...
十大招股说明书翻译公司排行榜公... 十大招股说明书翻译公司排行榜公开,领先者备受瞩目 招股说明书是企业上市过程中的核心法律文件,其翻译不...
日本大选后首次日债拍卖平稳落地... 智通财经APP注意到,日本国债连续一周走高,延续上涨行情。此前五年期国债拍卖平稳进行,支撑了市场积极...
原创 特... 即便特朗普定于四月启程访华,这并没有阻止他在临行前对中国采取强硬措施。比如,美方突然宣布,针对中国出...
原创 明... 稀土不在手,期货先开张。2026年刚开年,芝加哥商品交易所突然抛出重磅消息:要推出全球第一个稀土期货...
2026年新年献词|国联基金董... 来源:财联社 开栏语:马年新春至,辞旧启新程。回顾旧岁,证券业转型深化与公募业高质量发展并行,成...