当前位置: 首页 > news >正文

C#核心

类和对象

namespace 类和对象
{enum E_SexType{man,woman,}//成员变量,初始值:值类型默认都是0,引用类型默认都是null//访问修饰符public class Person(){public string name;public int age;public enum E_SexType;public Person[] friends;public void Speak(string str){Console.WriteLine("{0}说了{1}", name, str);}public bool IsAdult(){return age >= 18;}public void AddFriend(Person p){if(friends == null){friends = new Person[] { p };}else{Person[] NewFriends = new Person[friends.Length + 1];for (int i = 0; i < friends.Length; i++){NewFriends[i] = friends[i];}NewFriends[NewFriends.Length - 1] = p;friends = NewFriends;}}}internal class Program{static void Main(string[] args){Person p1 = new Person();p1.name = "aaa";p1.age = 20;p1.Speak("abc");Person p2 = new Person();p2.name = "ccc";p2.age = 16;p1.AddFriend(p2);for (int i = 0; i < p1.friends.Length; i++){Console.WriteLine(p1.friends[i].name);}Console.WriteLine( p1.IsAdult());Console.WriteLine(p1.age);  Console.WriteLine("Hello, World!");}}
}

封装_构造函数和析构函数

namespace 封装_构造函数和析构函数
{//构造函数//在实例化对象时,会调用的用于初始化的函数//如果不写,默认存在一个无参构造函数//1.没有返回值//2.函数名和类名必须相同//3.没有特殊需求时,一般都是public的class Person{public string name;public int age;public Person(){name = "aaa";age = 18;}public Person(int age){this.age = age;}public Person(string name){this.name = name;}//先调用namepublic Person(string name, int age):this(name){}//析构函数,引用类型的堆内存被回收时~Person(){}}//垃圾回收机制//垃圾分类的过程是在遍历堆(heap)上动态分配的所有对象//通过识别它们是否被引用来确定哪些对象是垃圾//GC只负责堆内存的回收//栈上的内存是由系统自动管理的internal class Program{static void Main(string[] args){Person p = new Person();Console.WriteLine("Hello, World!");//手动触发垃圾回收GC.Collect();}}
}

成员属性

namespace 成员属性
{class Person{private string name;private int age;private int money;private bool sex;//成员属性用于保护成员变量//为成员属性的获取和赋值添加逻辑处理public string Name{get{//可以在返回之前添加一些逻辑规则//get意味着这个属性可以获取的内容return name;}set{//可以在设置之前添加一些逻辑规则//value关键字用于表示外部传入的值name = value;}}public int Money{get{//解密处理return money-5;}set{//加密处理money = value+5;}}public bool Sex{get{return sex;}}//自动属性//没有在get和set中写逻辑的需求或者想法public float Height{get;set;}}internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");Person p = new Person();p.Name = "aaa";Console.WriteLine(p.Name);p.Money = 10;Console.WriteLine(p.Money);Console.WriteLine(p.Sex);}}
}

索引器

namespace 索引器
{class Person{private string name;private int age;private Person[] friends;//索引器写法public Person this[int index]{get{return friends[index];}set{friends[index] = value;}}}internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");}}
}
# 静态成员
namespace 静态成员
{class Test{//静态成员变量public static float PI = 3.14f;//成员变量public int testInt = 10;//静态成员方法//成员变量只能将对象实例化出来后,才能点出来使用,不能无中生有//不能直接使用非静态成员public static float Calc(float r){return PI * r * r;}}internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");//静态成员可以不实例化,直接调用//程序开始运行时,就会分配内存空间,所以能直接使用//静态成员和程序同生共死Console.WriteLine(Test.PI);Console.WriteLine(Test.Calc(2));}}
}

封装_静态类和静态构造函数

namespace 封装_静态类和静态构造函数
{//静态类,不能实例化static class TestStatic{public static int testIndex = 0;public static void Test(){}//静态构造函数static TestStatic(){}}internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");}}
}

拓展方法

namespace 拓展方法
{static class Tools{//为int拓展了一个成员方法//value代表使用该方法的实例化对象public static void SpeakValue(this int value){Console.WriteLine("为int拓展的方法" + value);}public static void SpeakString(this string str,string str2){Console.WriteLine("调用方法的对象" + str);Console.WriteLine("传的参数" + str2);}public static void Fun3(this Test t){Console.WriteLine("为test拓展的方法");}}//为自定义类型拓展方法class Test{public int i = 10;public void Fun1(){Console.WriteLine("123");}public void Fun2(){Console.WriteLine("456");}}internal class Program{static void Main(string[] args){Console.WriteLine("Hello, World!");int i = 10;i.SpeakValue();string str = "aaa";str.SpeakString("111");Test t = new Test();t.Fun3();}}
}

运算符重载

namespace 运算符重载
{class Point{public int x;public int y;//一定是一个公共的静态方法//返回值写在operator前面public static Point operator +(Point p1,Point p2){Point P = new Point();P.x = p1.x + p2.x;P.y = p1.y + p2.y;return P;}//一个运算符可以多次重载public static Point operator +(Point p1, int value){Point P = new Point();P.x = p1.x + value;P.y = p1.y + value;return P;}}internal class Program{static void Main(string[] args){Point p1 = new Point();p1.x = 1;p1.y = 2;Point p2 = new Point();p2.x = 3;p2.y = 4;Point p3 = new Point();p3 = p1 + p2;Point p4 = new Point();p4 = p1 + 2;}}
}

装箱拆箱

发生条件:
用object存值类型(装箱)
再把object转为值类型(拆箱)

基于里氏替换原则的object可以用object容器装载一切类型的变量
object是所有类型的基类

装箱
object v = 3;
拆箱
int intvalue = (int)v;

密封类

让类不能再被继承

sealed class Father
{}class Son:Father
{}

多态

多态:让同一类型的对象,执行相同行为时有不同的表现
解决的问题:让同一对象有唯一的行为特征
vob:
v:virtual 虚函数
o:override 重写
b:base 父类
v和o一定是结合使用的 来实现多态
b是否使用根据实际需求 保留父类行为

多态_抽象类和抽象方法

abstrct class Thing
{public string name;
}

抽象类不能被实例化
抽象方法1.必须在抽象类中声明2.没有方法体3.不能是私有的4.继承后必须实现,用override重写

不希望被实例化的对象,相对比较抽象的类可以使用抽象类
父类中的行为不太需要被实现的,只希望子类去定义具体的规则的 可以选择抽象类

接口

namespace 接口
{//接口是抽象行为的“基类”interface IFly{void Fly();string Name{get;set;}int this[int index]{get;set;}event Action dosomething;}class Animal{}//类可以继承一个类,多个接口//继承了接口之后,必须实现里面的内容,并且必须是public的class Person : Animal,IFly{//实现的接口函数,可以加virtual再在子类重写public virtual void Fly(){}public string Name{get;set;}public int this[int index]{get{return 0;}set{}}public event Action dosomething;}//接口继承接口时,不需要实现//待类继承接口后,类自己去实现所有内容interface IWalk{}interface IMove:IFly,IWalk{}//显示实现接口,就是用接口名.行为名实现interface IAtk{void Atk();}interface ISuperAtk{void Atk();}class Player:IAtk,ISuperAtk{void IAtk.Atk(){throw new NotImplementedException();}void ISuperAtk.Atk(){throw new NotImplementedException();}public void Atk(){}}internal class Program{static void Main(string[] args){Console.WriteLine("接口");//接口也遵循里氏替换原则IFly f = new Person();Player p = new Player();(p as IAtk).Atk();(p as ISuperAtk).Atk();p.Atk();}}
}

string常用方法

字符串指定位置读取

//字符串本质是char数组
string str = "aaa";
Console.WriteLine(str[0]);
//字符串拼接
str = string.Format("{0}{1},1,222333");
Console.WriteLine(str);
//正向查找字符位置
str = "abcde";
int index = str.IndexOf("a");
Console.WriteLine(index);
//移除指定位置后的字符
str = str.Remove(3);
http://www.jsqmd.com/news/298605/

相关文章:

  • C#进阶
  • 2026年市场上有名的打包带厂家排行,市场上有实力的打包带直销厂家广营宏利满足多元需求
  • BES(恒玄)蓝牙平台EQ 调试和设定
  • 将分散的Pytest测试脚本统一接入测试平台:FastAPI改造方案详解
  • 基于模糊控制的MATLAB避障算法实现
  • 为什么网络上搜索不到“桑桥网络”这家公司了?
  • 说说上海MNS2.0配电柜批量定制,如何选择厂家?
  • 2026年阜阳地区,为你分享专业的新能源汽修培训职业学校推荐
  • 自助KTV加盟哪家服务靠谱,长春鱼乐圈资料汇总
  • 2026年全国实力强的博士留学机构排名推荐,这些企业值得关注
  • 2025年成都火锅人气排行:3公里内口碑爆表的十大必吃店,牛肉火锅/麻辣烫/美食/市井火锅nbsp;成都火锅约会地点哪家好吃
  • 聊聊2026年别墅外墙砖靠谱厂家,广东和陶家居实力上榜
  • 读书笔记三:从需求到交付,坚守软件质量的核心底线
  • 金属带材环保电镀费用知多少,哪家收费合理?
  • 详细介绍:STM32外设学习--DMA直接存储器读取--学习笔记。
  • 基于STM32的智能宠物监控设计与实现
  • 基于STM32的智能家居安防系统
  • 基于STM32的智能导盲杖设计与实现
  • 基于STM32的智能楼梯灯系统
  • Vue 3 中的具名插槽仍然完全支持,Vue 2 的旧语法 Vue 3 中已废弃
  • 2026年全国靠谱的股权激励公司排名,创锟股权激励咨询实力入选值得关注
  • 鱼乐圈自助ktv音效好不好,分享值得选择的店铺排名
  • 盘点2026易切削钢专业厂家,宁波、杭州优质厂商Top10
  • 2026年重点关注金属制造企业,金轮精密的服务水平靠谱吗?
  • 详细介绍:Nature Communications|3D 打印仿生 SA-II 神经,让假肢感知拉伸
  • 2026年金属带材电镀源头厂家排名,重庆地区优质企业全揭秘
  • 读书笔记二:团队协作视角下,软件开发的流程与方法
  • 2026年地暖网片品牌深度评测与采购决策指南
  • Python篇---代码性能测试
  • Python篇---提升Python代码性能