c#中类似的Ctype代码

本文关键字:Ctype 代码 | 更新日期: 2023-09-27 17:52:12

此代码位于VB.NET

Dim obj As MyObject = CType(row.DataBoundItem, MyObject)

我想要一个类似的代码为c#

c#中类似的Ctype代码

var myObject = (MyObject)row.DataBoundItem;

或者更安全的方式:

var myObject = row.DataBoundItem as MyObject;
if(myObject != null)
{
    //do stuff
}

像这样:

var obj =(MyObject)row.DataBoundItem;

或:

MyObject obj =(MyObject)row.DataBoundItem;