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)文章
C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為T(mén)ext
這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為T(mén)ext,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
C# SendMail發(fā)送郵件功能實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C# SendMail發(fā)送郵件功能實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
WinForm項(xiàng)目開(kāi)發(fā)中WebBrowser用法實(shí)例匯總
這篇文章主要介紹了WinForm項(xiàng)目開(kāi)發(fā)中WebBrowser用法,需要的朋友可以參考下2014-08-08
C#從畫(huà)刷創(chuàng)建畫(huà)筆的方法
這篇文章主要介紹了C#從畫(huà)刷創(chuàng)建畫(huà)筆的方法,涉及C#圖形繪制的基本技巧,需要的朋友可以參考下2015-06-06
C#中WebBrowser.DocumentCompleted事件多次調(diào)用問(wèn)題解決方法
這篇文章主要介紹了C#中WebBrowser.DocumentCompleted事件多次調(diào)用問(wèn)題解決方法,本文講解了3種情況和各自情況的解決方法,需要的朋友可以參考下2015-01-01

