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

C# params基本語(yǔ)法及典型用法

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

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

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

一、params基本語(yǔ)法

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 必須是方法的最后一個(gè)參數(shù)。
  • 一個(gè)方法只能有一個(gè) params 參數(shù)。
  • 調(diào)用時(shí)可以直接傳多個(gè)值,也可以傳一個(gè)數(shù)組。

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

場(chǎng)景:檢查某個(gè)類(lèi)型是否實(shí)現(xiàn)了指定的一組接口

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

調(diào)用示例:

// 定義接口和類(lèi)
public interface IRunnable { }
public interface IFlyable { }
public class Bird : IRunnable, IFlyable { }
// 使用
Type birdType = typeof(Bird);
// 方式1:直接傳多個(gè) 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

三、其他常見(jiàn)用途

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

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

2. 注冊(cè)服務(wù)時(shí)指定多個(gè)接口

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. 斷言對(duì)象是否實(shí)現(xiàn)某些接口(單元測(cè)試)

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

四、注意事項(xiàng)

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

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

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

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

? 2. 類(lèi)型安全

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

? 正確:

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

? 錯(cuò)誤:

ISerializable obj = ...;
Check(obj); // 編譯錯(cuò)誤!obj 不是 Type 類(lèi)型

? 3. 性能

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

五、完整示例:通用接口驗(yàn)證工具

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);
    }
}
// 測(cè)試
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 是一種靈活接收多個(gè)接口類(lèi)型的寫(xiě)法。
  • 常用于反射、依賴(lài)注入、AOP、插件架構(gòu)等需要?jiǎng)討B(tài)處理類(lèi)型的場(chǎng)景。
  • 調(diào)用簡(jiǎn)潔,但需注意 null、性能和類(lèi)型安全。
  • 它讓 API 更友好:用戶(hù)無(wú)需手動(dòng)構(gòu)造數(shù)組。

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

相關(guān)文章

最新評(píng)論

桃江县| 台山市| 新民市| 宁明县| 承德市| 石嘴山市| 吴江市| 元谋县| 泽库县| 旅游| 镇远县| 炉霍县| 五莲县| 汕尾市| 建瓯市| 盐山县| 寿光市| 顺义区| 北宁市| 株洲市| 巴马| 获嘉县| 岫岩| 同心县| 轮台县| 怀安县| 巍山| 松溪县| 土默特右旗| 汶川县| 长顺县| 于田县| 唐海县| 岳阳县| 祥云县| 梓潼县| 宜都市| 鞍山市| 碌曲县| 永靖县| 福贡县|