10月7日學習筆記

C#中Sealed關鍵詞

 sealed 修飾符可以應用於類、實例方法和屬性。密封類不能被繼承。密封方法會重寫基類中的方法,但其本身不能在任何派生類中進一步重寫。當應用於方法或屬性時,sealed 修飾符必須始終與 override(C# 參考)一起使用。在類聲明中使用 sealed 修飾符可防止繼承此類

params 關鍵字

params 關鍵字可以指定在參數數目可變處採用參數的方法參數

在方法聲明中的 params 關鍵字之後不允許任何其他參數,並且在方法聲明中只允許一個 params 關鍵字。

示例

  複製代碼
// cs_params.cs
using System;
public class MyClass 
{

    public static void UseParams(params int[] list) 
    {
        for (int i = 0 ; i < list.Length; i++)
        {
            Console.WriteLine(list[i]);
        }
        Console.WriteLine();
    }

    public static void UseParams2(params object[] list) 
    {
        for (int i = 0 ; i < list.Length; i++)
        {
            Console.WriteLine(list[i]);
        }
        Console.WriteLine();
    }

    static void Main() 
    {
        UseParams(1, 2, 3);
        UseParams2(1, 'a', "test"); 

        // An array of objects can also be passed, as long as
        // the array type matches the method being called.
        int[] myarray = new int[3] {10,11,12};
        UseParams(myarray);
    }
}

輸出

 
1
2
3

1
a
test

10
11
12
發佈了66 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章