Thursday, February 22, 2024

C# DeleteFileWithRetry() - github copilot vs Me

 Gathering information about copilot writing C#.

CoPilot
     
//
public static bool DeleteFileWithRetry_CoPilot(string filename, int retryCount = 3, int sleepTime = 30)
{
    bool success = false;
    int retry = 0;
    while (!success && retry < retryCount)
    {
        try
        {
            System.IO.File.Delete(filename);
            success = true;
        }
        catch (System.IO.IOException)
        {
            System.Threading.Thread.Sleep(sleepTime);
            retry++;
        }
    }
    return success;
}
     
Me
//
public static bool DeleteFileWithRetry(string filename, int retryCount = 3, int sleepTime = 30) 
{
    for(var retry = 0; retry < retryCount; retry++)
    {
        try
        {
            if (File.Exists(filename))
                File.Delete(filename);
            return true;
        }
        catch (Exception ex)
        {
            Thread.Sleep(sleepTime * 1000);
        }
    }
    return false;
}
 

No comments:

Post a Comment