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)文章
C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text
這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08
WinForm項目開發(fā)中WebBrowser用法實例匯總
這篇文章主要介紹了WinForm項目開發(fā)中WebBrowser用法,需要的朋友可以參考下2014-08-08
C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法
這篇文章主要介紹了C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法,本文講解了3種情況和各自情況的解決方法,需要的朋友可以參考下2015-01-01

