C#中DirectCast的等价物是什么

时间:2018-04-27 02:47:12

标签: c# asp.net

以下是使用DirectCast的示例VB.NET代码。我无法将其转换为C#。

Dim arr As ArrayList
If ViewState("SelectedRecords") IsNot Nothing Then
    arr = DirectCast(ViewState("SelectedRecords"), ArrayList)
Else
    arr = New ArrayList()
End If

1 个答案:

答案 0 :(得分:4)

等效的C#代码是

ArrayList arr = default(ArrayList);
if (ViewState["SelectedRecords"] != null) {
    arr = (ArrayList)ViewState["SelectedRecords"];
}
else {
    arr = new ArrayList();
}