C#

168 阅读3分钟

C#自带特性

  • 序列化相关
    • 序列化:将对象转换为一种可存储或可传输的格式,例如JSON、XML或二进制格式,以便在需要时可以将其还原为原始对象。
    • 反序列化:反序列化是将序列化后的数据转换回原始对象的过程。
    • [JsonProperty("xxx")]
      • [JsonProperty("xxx")]是C#.NET中 Newtonsoft.Json 库中的一个特性,用于将类属性与JSON 字符串中的键名进行映射。例如,如果一个类有一个属性名为"Name",但是在JSON 字符串中对应的键名为"name",那么可以在该属性上添加[JsonProperty("name"]特性,使其能够正确地进行序列化和反序列化。这个特性在处理JSON 数据时非常有用,可以方便地将JSON 数据与 C# 对象进行转换。
      •    class a{
               [JsonProperty("msg_id")]
               public string MessageId { get; set; }
               
               public override string ToString()
               {
                    return JsonConvert.SerializeObject(this);
               }
           }
           
        Console.WriteLine("------------测试JSON序列化特性-----------");
            TestJSON testJSON = new TestJSON();
            Console.WriteLine(JsonConvert.SerializeObject(testJSON));  // {"msg_id":null}
            
            string json = JsonConvert.SerializeObject(new Dictionary<string, string>
            {
                { "msg_id","111" }
            });   
            TestJSON t1 = JsonConvert.DeserializeObject<TestJSON>(json);
            Console.WriteLine(t1);  // {"msg_id":"111"}
            
            string json1 = JsonConvert.SerializeObject(new Dictionary<string, string>
            {
                { "MessageId","111" }
            });
            TestJSON t2 = JsonConvert.DeserializeObject<TestJSON>(json1);
            // {"msg_id":null}   反序列化时,json字符的MessageId要是msg_id
            Console.WriteLine(t2);  
        Console.WriteLine("-----------------------");
        

C# 开发中疑惑的点

  • 方法参数

    以实参形式传入方法中的引用数据类型,当方法内修改此参数的值,实参是会发生变化的。

    同样适用于class与struct,实例类为引用类型 struct为值类型

    • in: 将实参以引用类型形式传递,方法内只能对实参进行读取不能修改。 性能优化 !!!
    • out: 将实参以引用类型传递,方法内必须要修改实参。
    • ref:将实参以引用类型方式传递,可读可写

    当传递的实参为引用类型时,在方法内修改实参的项或key时会使实参本体也发生变化。

    但如果直接新生成一个引用类型赋值给实参的话,就不会改变实参了。 这种情况下也可以使用ref,达到改变实参的效果

    int[] arr = { 1, 4, 5 };
    System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr[0]);
    
    Change( arr);
    System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);     
    
    Change1(ref arr);
    System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr[0]);
    
    static void Change(int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] { -3, -1, -2, -3, -4 };   // This change is local.
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }
    static void Change1(ref int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] { -13, -11, -12, -13, -14 };   // This change is local.
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }
    
    int n = 5;
    System.Console.WriteLine("The value before calling the method: {0}", n);
    
    SquareIt(ref n);  // Passing the variable by reference.
    System.Console.WriteLine("The value after calling the method: {0}", n);
    
    // Keep the console window open in debug mode.
    System.Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
    
    static void SquareIt(ref int x)
    // The parameter x is passed by reference.
    // Changes to x will affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);
    }
    /* Output:
        The value before calling the method: 5
        The value inside the method: 25
        The value after calling the method: 25
    */