查缺补漏
合并枚举 [Flags]
参考: Flags 特性 c#_c# flags属性-CSDN博客
[Flags] public enum Options { Insetred = 1, Update = 2, Query = 3, Delete = 4, ALL = Insetred | Update | Query | Delete }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | User user = new User(); user.Option = Options.Delete | Options.Update; Console.WriteLine(user.Option.ToString()); //是否包含 枚举 if (user.Option.HasFlag(Options.Update)) { Console.WriteLine( "用户可以更新" ); } User user1 = new User(); user1.Option = Options.ALL; Console.WriteLine(user1.Option); if (user1.Option.HasFlag(Options.Delete)) { Console.WriteLine( "用户可以删除" ); } |
C#解构器
参考: C#解构器_c# deconstruct-CSDN博客
关键字 Deconstruct 使用 Deconstruct(out DateTime createTime, out string orderCode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Order { public Order(DateTime createTime, string orderCode) { CreateTime = createTime; OrderCode = orderCode; } public DateTime CreateTime { get ; set ; } public string OrderCode { get ; set ; } public void Deconstruct( out DateTime createTime, out string orderCode) { createTime = CreateTime; orderCode = OrderCode; } } |
使用 (DateTime createTime, string orderCode) = order;
1 2 3 | Order order = new Order(DateTime.Now, "订单001" ); (DateTime createTime, string orderCode) = order; Console.WriteLine($ "订单生成时间:{createTime.ToString(" yyyy-MM-dd HH-mm-ss ")},订单号:{orderCode}" ); |
implicit 隐式转换 explicit 显示转换
静态 ,关键字 operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Student { public Student( string all) { All = all; } public string All { get ; set ; } public static implicit operator Student(Person person) { return new Student(person.FirstName + " " + person.LastName); } public static explicit operator Person(Student student) { return new Person() { FirstName = student.All }; } } |
使用
1 2 3 4 5 6 7 8 | Console.WriteLine( "====隐式转换=====" ); //隐式转换 Student student = person; Console.WriteLine(student.All); //显示转换 Person person3 = (Person)student; Console.WriteLine( "====显示转换=====" ); Console.WriteLine(person3.FirstName); |
ICollection
ICollection 接口是 System.Collections 命名空间中类的基接口,ICollection 接口扩展 IEnumerable,IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 ICollection 接口派生新集合类以提高灵活性。
ICollection是IEnumerable的加强型接口,它继承自IEnumerable接口,提供了同步处理、赋值及返回内含元素数目的功能
IEnumerable接口
1、IEnumerable接口是ICollection的父接口,凡实现此接口的类,都具备“可迭代”的能力。
2、IEnumerable接口只定义了一个方法:GetEnumerator,该方法将返回一个“迭代子”对象(或称为迭代器对象),是一个实现了IEnumerator接口的对象实例。
3、凡是实现了IEnumerable接口的类,都可以使用foreach循环迭代遍历。
4` 可以使用yield return 跳出循环 yield break
1 2 3 4 5 | public IEnumerable< string > GetNames() { yield return "汤姆" ; yield return "吉瑞" ; } |
使用
1 2 3 4 | Console.WriteLine( "====使用yied return=====" ); List< string > names = order.GetNames().ToList(); Console.WriteLine( string .Join( "," , names)); Console.WriteLine( "====使用yied return end=====" ); |
c# DateTimeOffset DateTime TimeSpan 不同点
比较与日期和时间相关的类型 - .NET | Microsoft Learn
C#中 DateTime , DateTime2 ,DateTimeOffset 之间的小区别 (转载)-CSDN博客
事件
1第一种先 定义委托
1 2 3 4 5 6 7 8 9 10 11 12 13 | /// <summary> /// 订单创建状态 /// </summary> /// <param name="orderCode"></param> /// <returns></returns> public delegate bool OrderStartDelegate( string orderCode); /// <summary> /// 订单关闭状态 /// </summary> /// <param name="orderCode"></param> /// <returns></returns> public delegate bool OrderCancelDelegate( string orderCode); |
再定义 事件
1 2 | OrderStartDelegate orderStartDelegate; public event OrderStartDelegate OrderStartEvent { add { orderStartDelegate += value; } remove { orderStartDelegate -= value; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Order { //第一种 OrderStartDelegate orderStartDelegate; public event OrderStartDelegate OrderStartEvent { add { orderStartDelegate += value; } remove { orderStartDelegate -= value; } } OrderCancelDelegate orderCancelDelegate;<br> //定义事件 public event OrderCancelDelegate OrderCancelEvent { add { orderCancelDelegate += value; } remove { orderCancelDelegate -= value; } } public event EventHandler<OrderPaymentSucceededArgs> OrderPaymentSucceededEvent; /// <summary> /// 创建订单 /// </summary> /// <returns></returns> public bool OrderStart() { //创建订单 bool ? @ bool = orderStartDelegate?.Invoke(OrderCode); return @ bool .Value; }<br>} |
第二种
先定义事件参数
1 2 3 4 5 6 7 8 9 10 11 12 | /// <summary> /// 订单支持成功状态参数 /// </summary> public class OrderPaymentSucceededArgs : EventArgs { public OrderPaymentSucceededArgs( string orderCode) { OrderCode = orderCode; } public string OrderCode { get ; set ; } } |
再定义事件
1 2 3 4 5 6 7 8 9 10 | public class Order { public event EventHandler<OrderPaymentSucceededArgs> OrderPaymentSucceededEvent; public bool OrderPaymentSucceede() { OrderPaymentSucceededEvent?.Invoke( this , new OrderPaymentSucceededArgs(OrderCode)); return true ; }<br>} |
匿名类型
1 2 3 | Console.WriteLine( "=== 匿名类型=====" ); var address = new { street = "00号大街" }; Console.WriteLine(address.street); |
linq
into 只能出现在select group 后边
1 | into <strong>custormPurchases 为 </strong>purchase 序列 |
1 | from cp in custormPurchases.DefaultIfEmpty()<br>为 展开 <strong>custormPurchases</strong> |
1 2 3 4 | var query = from custorm in custorms join purchase in purchases.Where(item=>item.Price>5000) on custorm.Id equals purchase.CustormId into custormPurchases from cp in custormPurchases.DefaultIfEmpty() select new { custormName = custorm.Name, price = cp?.Price, productName = cp?.Name }; |
深入理解多线程中的yield方法
volatile
olatile是C#中用于控制同步的关键字,其意义是针对程序中一些敏感数据,不允许多线程同时访问,保证数据在任何访问时刻,最多有一个线程访问,以保证数据的完整性,volatile是修饰变量的修饰符。
1volatile的使用场景
多个线程同时访问一个变量,CLR为了效率,允许每个线程进行本地缓存,这就导致了变量的不一致性。volatile就是为了解决这个问题,volatile修饰的变量,不允许线程进行本地缓存,每个线程的读写都是直接操作在共享内存上,这就保证了变量始终具有一致性。
2、volatile 关键字可应用于以下类型的字段
(1)、引用类型
(2)、整型,如 sbyte、byte、short、ushort、int、uint、char、float 和 bool。
(3)、具有整数基类型的枚举类型。
(4)、已知为引用类型的泛型类型参数。
(5)、不能将局部变量声明为 volatile。
恐怕比较一下volatile和synchronized的不同是最容易解释清楚的。volatile是变 量修饰符,而synchronized则作用于一段代码或方法;看如下三句get代码:
- int i1; int geti1() {return i1;}
- volatile int i2; int geti2() {return i2;}
- int i3; synchronized int geti3() {return i3;}
AssemblyDescriptionAttribute
是 .NET 框架中的一个特性类,用于为程序集提供描述信息。
Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>();
.NetCore MemoryCache使用详解
1.ExpirationScanFrequency获取或设置对过期项的连续扫描之间的最短时间间隔
2.SizeLimit 缓存是没有大小的的,此值设置缓存的份数
3.CompactionPercentage获取或设置在超过最大大小时压缩缓存的数量,优先压缩优先级较低的缓存,0.2代表20%
1 2 3 4 5 6 7 8 9 | services.AddMemoryCache(options => { // 缓存最大为100份 //##注意netcore中的缓存是没有单位的,缓存项和缓存的相对关系 options.SizeLimit = 2; //缓存满了时候压缩20%的优先级较低的数据 options.CompactionPercentage = 0.2; //两秒钟查找一次过期项 options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); }); |
1 2 3 4 5 6 7 | private static readonly IMemoryCache _memoryCache = new MemoryCache(Options.Create( new MemoryCacheOptions { //缓存满了时候压缩20%的优先级较低的数据 CompactionPercentage = 0.05, //1秒钟查找一次过期项 ExpirationScanFrequency = new TimeSpan(0, 0, 1), })); |
SemaphoreSlim是一个用于同步和限制并发访问的类,和它类似的还有Semaphore,只是SemaphoreSlim更加的轻量、高效、好用。今天说说它,以及如何使用,在什么时候去使用,使用它将会带来什么优势。
static SemaphoreSlim semaphore = new SemaphoreSlim(1); //控制访问线程的数量
semaphore.Wait();
semaphore.Release();
services.AddMemoryCache(options => { // 缓存最大为100份 //##注意netcore中的缓存是没有单位的,缓存项和缓存的相对关系 options.SizeLimit = 2; //缓存满了时候压缩20%的优先级较低的数据 options.CompactionPercentage = 0.2; //两秒钟查找一次过期项 options.ExpirationScanFrequency = TimeSpan.FromSeconds(2); });
public interface IAnmail<T> { T GetName(); } public class Lion : IAnmail<string> { public string GetName() { return "狮子"; } } Lion lion = new Lion(); Array array = new[] { 22, 33, }; Type arrayType = typeof(Array).MakeArrayType(1); var arresult = arrayType.GetMethod("GetValue", new[] { typeof(int) }).Invoke(array, new object[] { 0 }); Type anmailType = typeof(IAnmail<>).MakeGenericType(typeof(string)); object anmailResult = anmailType.GetMethod("GetName").Invoke(lion, null); Console.WriteLine(anmailResult.ToString()); MakeGenericType 是 .NET 反射中用于构造泛型类型的关键方法。
它允许你将一个开放泛型类型(未指定具体类型参数的泛型类型,如 List<>)转换为封闭泛型类型(指定了具体类型参数的泛型类型,如 List<string>)。
以下是详细用法和示例: 基本概念 开放泛型类型 (Open Generic Type) 泛型类型定义时未指定具体类型参数,例如 List<>、Dictionary<,> 或你示例中的 IAnimal<>。 封闭泛型类型 (Closed Generic Type) 泛型类型定义时已指定具体类型参数,例如 List<string>、Dictionary<int, string> 或 IAnimal<string>。 MakeGenericType 的作用 将开放泛型类型绑定到具体类型参数,生成一个新的 Type 对象。 常用于动态操作泛型类型(例如通过反射创建泛型实例或调用泛型方法)。
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现