Why is this C # code not compiling?

Why is this C # code not compiling?

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;
}

Errors:

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

I can't think of a case where bardatetime and chunksize will end up unassigned.

Change I fixed this error by setting the code to logically equivalent. Honestly, I wanted to avoid multiple attempts.

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;
        chunksize = currentCard.ChunkSize;
        bardatetime = currentCard.CurrentBarDateTime;

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

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

        return result;
    }
+3
source share
11 answers

If the while and "if statement" loops are never entered, out parameters are not assigned.

It may be logical that these code codes will always be entered. The compiler does not know this. The compiler believes that all if and while that have an unstable state can be entered or skipped.

. "before", , , "if" out. null, , , " , out".

, , , , , , , , , , , .

+18

out . , , while (currentCard.CurrentSecond <= startSecond) if (result = null), .

, while , . do {//logic} while (//condition);.

, out.

if (currentCard.CurrentSecond <= startSecond)
{
  while (currentCard.CurrentSecond <= startSecond)
  {
    result = GetBulk(bufferId, out chunksize, out bardatetime);
  }
}
else
{
  result = null;
}
+5
if currentCard.CurrentSecond > startSecond 

if result is null

.

- :

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;

    // initialize with a -1
    bardatetime = -1;
    chunksize = -1;   

    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;
}
+3

, chunksize bardatetime, - (while if), , .

, , , .

+2

, , , ( , ) .

+1

result null, bardatetime . , out, . , .

+1

currentCard.CardSecond <= startSecond while , null, . , .CardSecond startSecond?

0

while() {} do {} while() - loop. .

0

, , , , GetBulk.

0

.

0

, ReSharper , .

0

Source: https://habr.com/ru/post/1766773/


All Articles