.NET相关知识(一)

本文深入探讨了C#与.NET平台的核心概念,包括公共语言基础结构(CLI)、托管与非托管资源的区别、文件I/O操作、类型转换、时间格式处理、字符串解析、编码、异常处理、垃圾回收、正则表达式、泛型编程、委托、LINQ、并行处理及并发编程等关键主题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 公共语言基础结构(CLI)
  2. 如果某个程序仅访问分配的内存,则该程序就是内存安全的
  3. 未托管的资源是指不由 .NET 运行时自动维护的资源。 例如,文件句柄就是未托管的资源。 FileStream 对象是一个托管对象,但它引用未托管的文件句柄。
  4. 在文件I/O中,要从一个文件读取数据,应用程序首先要调用操作系统函数并传送文件名,并选一个到该文件的路径来打开文件。该函数取回一个顺序号,即文件句柄(file handle),该文件句柄对于打开的文件是唯一的识别依据。
  5. .NET运行时(.NET Framework 公共语言运行时 (CLR)其中一种);
  6. .NET Standard 将取代可移植类库 (PCL)。
  7. AOT预编译器,IL中间语言,JIT实时编译器
  8. 托管代码(简而言之,托管代码就是执行过程交由运行时管理的代码。 )
  9. 为非托管资源释放内存(实现IDispose接口包括文件句柄、窗口句柄或网络连接)
  10. Convert转换相关
        int j = checked(3);
        int k = Convert.ToInt32(false);
        int codePoint = 1067;
        IConvertible iConv = codePoint;
        char ch = iConv.ToChar(null);
        //使用 ChangeType 方法的自定义转换,Convert.ChangeType(Object, Type, IFormatProvider) 方法执行的
  1. 时间相关
        //string customFormat = "MMMM dd,yyyy (dddd)";
        //DateTime data1 = new DateTime(2019, 1, 7);
        //print(data1.ToString(customFormat));

        //int value = 123;
        //print(value.ToString("D"));
        //foreach(var customString in DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns('d'))
        //{
        //    print(customString);
        //}

        TimeSpan duration = new TimeSpan(1, 12, 23, 62);
        TimeSpan interval;
        string value = "1.03:14:56.1667";

        try
        {
            interval = TimeSpan.ParseExact(value, "c", null);
        }
        catch (FormatException)
        {

        }
        catch (OverflowException)
        {

        }

        TimeSpan interval2 = TimeSpan.FromTicks(22233);方法执行的
  1. 字符串相关
        string dateInput = "Jan 1, 2009";
        DateTime parsedDate = DateTime.Parse(dateInput);

        string MyString3 = "Thursday";
        DayOfWeek MyDays = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), MyString3);
  1. 编码相关
        Encoding asciiEncoding = Encoding.ASCII;
        //asciiEncoding.GetByteCount();
        //asciiEncoding.GetString();
        //asciiEncoding.GetCharCount();
  1. 异常相关
        try
        {
            StreamReader sr = File.OpenText("data.txt");

            Console.WriteLine("The first line of this file is {0}", sr.ReadLine());
            sr.Close();
        }
        catch (Exception e)
        {

            print(e.Message);
           // throw new Exception();
        }
        //finally
        //{

        //}
  1. 垃圾回收(待补充)
  2. 正则表达式(待补充)
  3. 泛型相关
    public int IComparer<T>() { return 0; }
    public delegate bool Predicate<T>(T item);
    
    struct Sk
    {
        int ww;
    }
    public struct Nullable<T> where T:struct
    {
     
    }
    class Test<T,U> where U:struct where T : new()
    {

    }

    public class SampleClass<T,U,V>where T : V
    {

    }

    class BaseNode { }
    class BaseNodeGeneric<T> { }

    // concrete type
    class NodeConcrete<T> : BaseNode { }

    //closed constructed type
    class NodeClosed<T> : BaseNodeGeneric<int> { }

    //open constructed type 
    class NodeOpen<T> : BaseNodeGeneric<T> { }

    public delegate void Del<T>(T item);
    public static void Notify(int i) { }
    Del<int> m1 = new Del<int>(Notify);

    class Stack<T>
    {
        T[] items;
        int index;

        public delegate void StackDelegate(T[] items);
    }

    void Start()
    {
        Nullable<Sk> NI = new Nullable<Sk>();
        int wo = IComparer<string>();
        Predicate<int> bill = new Predicate<int>(wwwl);
        print(NI);
        print(wo);
        SampleClass<int, string, int> gg;
        //约束相关 struct class unmanaged new() <基类名> <接口名> U
        //某些约束是互斥的。 所有值类型必须具有可访问的无参数构造函数。 struct 约束包含 new() 约束,且 new() 约束不能与 struct 约束结合使用。 unmanaged 约束包含 struct 约束。 unmanaged 约束不能与 struct 或 new() 约束结合使用。
    }
  1. 委托相关
    public class Point
    {
        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
        public int X;
        public int Y;
    }

    private void Start()
    {
        Point[] points = { new Point(100, 200),
                         new Point(150, 250), new Point(250, 375),
                         new Point(275, 395), new Point(500, 650) };

        Predicate<Point> predicate = FindPoints;

        Point[] first = Array.FindAll(points, predicate);
        foreach (var item in first)
        {
            print(string.Format("found: x={0},y={1}", item.X, item.Y));
        }
    }

    private bool FindPoints(Point obj)
    {
        return obj.X * obj.Y > 100000;
    }

  1. LINQ(待补充)

  1. 并行处理、并发和异步编程(待补充)
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值