如何设置初始化变量c#?

如何设置初始化变量c#?,c#,C#,我是c#的新手,有一个变量: var updates = await Bot.GetUpdatesAsync(offset); 但现在需要更新定义其他范围并将其用于此形状: updates = await Bot.GetUpdatesAsync(offset); 尝试使用以下代码定义: var updates =(string) null; 但是在这一行中,c#编译器得到了这个错误: updates = await Bot.GetUpdatesAsync(offset); (

我是c#的新手,有一个变量:

var updates = await Bot.GetUpdatesAsync(offset);

但现在需要更新定义其他范围并将其用于此形状:

updates = await Bot.GetUpdatesAsync(offset);

尝试使用以下代码定义:

var updates =(string) null;

但是在这一行中,c#编译器得到了这个错误:

    updates = await Bot.GetUpdatesAsync(offset);
(awaitable)Task<Update[]> TelegramBotClient.GetUpdatesAsync([int offset=0],[int limit=100],[int timeout=0],[CancellationToken cancellationToken=default(CancellationToken)
use this method recieve incoming updates using long polling.
Usage:
Update[] x=await GetUpdateAsync(...);
Can not implicitly convert type 'Telegram.Bot.Types.Update[]' to 'string'

显然,方法的返回类型不是
字符串
,而是
电报.Bot.Types.Update[]

因此,您可以将代码更改为

Telegram.Bot.Types.Update[] updates;
// the rest of code
updates = await Bot.GetUpdatesAsync(offset);

替换
var更新=(字符串)null带有
var updates=(Telegram.Bot.Types.Update[])null

当然,如果您将
var
设置为
string
它将不会将
Telegram.Bot.Types.Update[]
转换为
string
,并将抛出错误。感谢您的帮助,我的朋友使用
var
关键字隐式键入的目的是使代码更短,更可读。但是
var updates=(Telegram.Bot.Types.Update[])null比显式键入(如
Telegram.Bot.Types.Update[]updates)可读性差,时间长
,因此我不建议以这种方式使用
var
。同意Andy。我们可以将其声明为Telegram.Bot.Types.Update[]updates;
Telegram.Bot.Types.Update[] updates;
// the rest of code
updates = await Bot.GetUpdatesAsync(offset);