小编Mos*_*lan的帖子

如何在C#中更改变量类型?

我想用这样的东西:

if(x==5)
{
    var mydb= ........ ;
}
else 
{
    var mydb = ........ ;
}
Run Code Online (Sandbox Code Playgroud)

但它没有用,因为我无法在if语句中声明变量.

所以我试着这样做:

var mydb;

if (x==5)
{
    mydb= ............. ;
}
else 
{
    mydb=.............;
}
Run Code Online (Sandbox Code Playgroud)

但是id不起作用,因为我必须初始化变量(mydb).

所以问题是:我不一定知道变量的类型,我是否可以声明它然后更改if语句中的类型?

c# variables types

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

c# ×1

types ×1

variables ×1