如何设置初始化变量c#?
我是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); (
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);