0
如何在不同條件下返回類對象。根據條件返回類對象
假設我的結果是成功返回認證,否則返回類錯誤。
public Class Example
{
public Authentication GetDetails(int id)
{
Authentication objAuth = new Authentication();
if(id==100)
{
return objAuth ;
}
else
{
ClassB objB=new ClassB();
return objB;
}
}
}
static Void Main()
{
Example obj = new Example();
obj.GetDetails();
}
您應該使用例外。 – SLaks
您正在重塑一個方形的車輪。如果認證失敗,請考慮拋出異常。 –
至於常見場景中的例外https://docs.microsoft.com/en-us/dotnet/standard/exceptions/best-practices-for-exceptions#handle-common-conditions-without-throwing-exceptions –