加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么这个C#代码没有编译?

发布时间:2020-12-15 20:53:17 所属栏目:百科 来源:网络整理
导读:为什么这个C#代码没有编译? public static Dictionaryshort,MemoryBuffer GetBulkCustom(int bufferId,int startSecond,out int chunksize,out int bardatetime){ //const string _functionName = "GetNextBulkWatchData"; UserSeriesCard currentCard = Ge
为什么这个C#代码没有编译?

public static Dictionary<short,MemoryBuffer> GetBulkCustom(int bufferId,int startSecond,out int chunksize,out int bardatetime)
{
    //const string _functionName = "GetNextBulkWatchData";

    UserSeriesCard currentCard = GetUserSeriesCard(bufferId);

    Dictionary<short,MemoryBuffer> result = null;

    while (currentCard.CurrentSecond <= startSecond)
        result = GetBulk(bufferId,out chunksize,out bardatetime);

    if (result == null)
    {
        result = currentCard.UserBuffer;
        chunksize = currentCard.ChunkSize;
        bardatetime = currentCard.CurrentBarDateTime;
    }
    return result;
}

错误:

The out parameter 'bardatetime' must be assigned to before control leaves the current method
The out parameter 'chunksize' must be assigned to before control leaves the current method

我想不出bardatetime和chunksize最终会被取消分配的情况.

编辑.我通过将代码调整为逻辑等效的代码来修复此错误.老实说,我想避免多重分配.

public static Dictionary<short,out int bardatetime )
    {
        const string _functionName = "GetNextBulkWatchData";

        UserSeriesCard currentCard = GetUserSeriesCard(bufferId);

        Dictionary<short,MemoryBuffer> result = null;
        chunksize = currentCard.ChunkSize;
        bardatetime = currentCard.CurrentBarDateTime;

        while (currentCard.CurrentSecond <= startSecond)
            result = GetBulk(bufferId,out bardatetime);

        if (result == null)
            result = currentCard.UserBuffer;

        return result;
    }

解决方法

如果从未输入while循环和“if语句”主体,则不分配out参数.

从逻辑上讲,您可能知道始终会输入这些代码路径.编译器不知道这一点.编译器认为可以输入或跳过具有非常数条件的每个“if”和“while”.

在这种情况下,编译器可以进行更复杂的流分析.分析是“在’if’之前,结果为null或非null;如果为null,则’if’主体分配out参数.如果它不为null,那么唯一可能发生的方法是’如果’ ‘body指定了out参数,因此out参数被赋值.“

这种分析水平当然是可能的,但规范中描述的现有流量分析算法具有一些很好的特性,即它快速,易于理解,易于实现,通常是准确的,并且只给出误报,而不是假阴性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读