最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# params基本語法及典型用法

 更新時間:2025年12月16日 11:10:03   作者:她說彩禮65萬  
C#中的params關(guān)鍵字用于定義可變參數(shù)列表,允許方法接收任意數(shù)量的指定類型參數(shù),它常用于反射、依賴注入和插件系統(tǒng)等場景,本文介紹C# params基本語法及典型用法,感興趣的朋友跟隨小編一起看看吧

在 C# 中,params 關(guān)鍵字用于定義**可變參數(shù)列表(variable-length argument list)**的方法參數(shù)。它允許調(diào)用者傳入 0 個或多個指定類型的參數(shù),而無需顯式創(chuàng)建數(shù)組。

你提到的 params Type[] interfaceTypes 是一個典型的使用場景:方法接收任意數(shù)量的 Type 對象(通常表示接口類型),用于反射、依賴注入、插件系統(tǒng)等。

一、params基本語法

public void MyMethod(params int[] numbers)
{
    foreach (int n in numbers)
        Console.WriteLine(n);
}
// 調(diào)用方式:
MyMethod();           // numbers = new int[0]
MyMethod(1);          // numbers = new int[] { 1 }
MyMethod(1, 2, 3);    // numbers = new int[] { 1, 2, 3 }

? 規(guī)則

  • params 必須是方法的最后一個參數(shù)
  • 一個方法只能有一個 params 參數(shù)。
  • 調(diào)用時可以直接傳多個值,也可以傳一個數(shù)組。

二、params Type[] interfaceTypes的典型用法

場景:檢查某個類型是否實現(xiàn)了指定的一組接口

public static bool ImplementsAllInterfaces(Type targetType, params Type[] interfaceTypes)
{
    if (interfaceTypes == null || interfaceTypes.Length == 0)
        return true; // 沒有要求接口,視為滿足
    var implementedInterfaces = targetType.GetInterfaces();
    foreach (var iface in interfaceTypes)
    {
        if (!implementedInterfaces.Contains(iface))
            return false;
    }
    return true;
}

調(diào)用示例:

// 定義接口和類
public interface IRunnable { }
public interface IFlyable { }
public class Bird : IRunnable, IFlyable { }
// 使用
Type birdType = typeof(Bird);
// 方式1:直接傳多個 Type
bool result1 = ImplementsAllInterfaces(birdType, typeof(IRunnable), typeof(IFlyable));
// 方式2:傳數(shù)組(等效)
Type[] required = { typeof(IRunnable), typeof(IFlyable) };
bool result2 = ImplementsAllInterfaces(birdType, required);
// 方式3:不傳(空參數(shù))
bool result3 = ImplementsAllInterfaces(birdType); // 返回 true

三、其他常見用途

1. 動態(tài)創(chuàng)建實現(xiàn)多個接口的代理(如 Castle DynamicProxy)

proxyGenerator.CreateClassProxy(
    typeof(MyClass),
    new[] { typeof(IInterceptor) },
    params Type[] additionalInterfacesToProxy // ← 這里常用 params
);

2. 注冊服務(wù)時指定多個接口

public void RegisterService(Type implementation, params Type[] serviceTypes)
{
    foreach (var service in serviceTypes)
    {
        container.Register(service, implementation);
    }
}
// 調(diào)用
RegisterService(typeof(Logger), typeof(ILogger), typeof(IDisposable));

3. 斷言對象是否實現(xiàn)某些接口(單元測試)

public void AssertImplements(object obj, params Type[] expectedInterfaces)
{
    Type actualType = obj.GetType();
    foreach (var iface in expectedInterfaces)
    {
        Assert.IsTrue(actualType.GetInterfaces().Contains(iface));
    }
}

四、注意事項

? 1.params參數(shù)可以為null

MyMethod(null); // 此時 params 數(shù)組為 null!

因此在方法內(nèi)部應(yīng)做空值檢查:

public void Foo(params string[] args)
{
    if (args == null) 
    {
        // 處理 null 情況
    }
}

? 2. 類型安全

params Type[] 要求傳入的每個參數(shù)必須是 Type 類型(通常是 typeof(接口)),不能傳接口實例。

? 正確:

Check(typeof(ISerializable), typeof(IDisposable));

? 錯誤:

ISerializable obj = ...;
Check(obj); // 編譯錯誤!obj 不是 Type 類型

? 3. 性能

每次調(diào)用會隱式創(chuàng)建數(shù)組(除非傳入已有數(shù)組),高頻調(diào)用需注意分配開銷。

五、完整示例:通用接口驗證工具

using System;
using System.Linq;
public static class InterfaceChecker
{
    public static bool HasAllInterfaces(Type type, params Type[] requiredInterfaces)
    {
        if (requiredInterfaces == null || requiredInterfaces.Length == 0)
            return true;
        var implemented = type.GetInterfaces();
        return requiredInterfaces.All(implemented.Contains);
    }
}
// 測試
interface IA { }
interface IB { }
class MyClass : IA, IB { }
class Program
{
    static void Main()
    {
        bool ok = InterfaceChecker.HasAllInterfaces(
            typeof(MyClass),
            typeof(IA),
            typeof(IB)
        );
        Console.WriteLine(ok); // True
    }
}

總結(jié)

  • params Type[] interfaceTypes 是一種靈活接收多個接口類型的寫法。
  • 常用于反射、依賴注入、AOP、插件架構(gòu)等需要動態(tài)處理類型的場景。
  • 調(diào)用簡潔,但需注意 null、性能和類型安全。
  • 它讓 API 更友好:用戶無需手動構(gòu)造數(shù)組。

到此這篇關(guān)于C# params基本語法及典型用法的文章就介紹到這了,更多相關(guān)c# params使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

赤峰市| 辛集市| 盐津县| 辽中县| 扎鲁特旗| 安仁县| 台东市| 阿克| 玉林市| 高邮市| 巴马| 大兴区| 遂昌县| 天津市| 万宁市| 江西省| 陆良县| 霍林郭勒市| 游戏| 如皋市| 南江县| 锡林郭勒盟| 东乡族自治县| 涞水县| 昌乐县| 儋州市| 慈溪市| 陆丰市| 太仆寺旗| 利川市| 盐源县| 天水市| 寿阳县| 房产| 五莲县| 永宁县| 鄂伦春自治旗| 兴义市| 阳江市| 富阳市| 沙田区|