Ho to use/enable #nullable in c# winforms .net 4.8?

public event EventHandler<DownloadeCompletedEventArgs>? DownloadCompleted;
public event EventHandler<DownloadProgressEventArgs>? DownloadProgress;

getting warning on both ‘?’

The annotation for nullable reference types should only be used in code within a ‘#nullable’ annotations context.

should I enable somehow the nullable? or just checking when need to check that it’s not null?

when should I use #nullable?

what more details should i provide? i provided all the information needed.
I want to know how to handle this warnings.

This is the class code:

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Downloading_Files
{
    public class DownloadingCore
    {
        public class DownloadeCompletedEventArgs : EventArgs
        {
            public bool IsSuccess { get; set; }
            public bool IsCancelled { get; set; }
            public string FilePath { get; set; } = string.Empty; // Initialize with an empty string
        }

        public class DownloadProgressEventArgs : EventArgs
        {
            public int Percentage { get; set; }
        }

        public class DownloadManager
        {
            public long ContentLength { get; private set; } // Expose ContentLength as a property

            public event EventHandler<DownloadeCompletedEventArgs>? DownloadCompleted;
            public event EventHandler<DownloadProgressEventArgs>? DownloadProgress;

            public async Task StartDownload(string url, string filePath, CancellationToken cancellationToken)
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
                        response.EnsureSuccessStatusCode();

                        ContentLength = response.Content.Headers.ContentLength.GetValueOrDefault(); // Set ContentLength

                        var totalBytesRead = 0L;

                        using (var contentStream = await response.Content.ReadAsStreamAsync())
                        using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                        {
                            var buffer = new byte[8192];
                            int bytesRead;
                            while ((bytesRead = await contentStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0)
                            {
                                await fileStream.WriteAsync(buffer, 0, bytesRead, cancellationToken);
                                totalBytesRead += bytesRead;

                                if (ContentLength > 0)
                                {
                                    var progressPercentage = (int)((float)totalBytesRead / ContentLength * 100);
                                    OnDownloadProgress(new DownloadProgressEventArgs { Percentage = progressPercentage });
                                }
                            }
                        }

                        OnDownloadCompleted(new DownloadeCompletedEventArgs { IsSuccess = true, FilePath = filePath });
                    }
                }
                catch (OperationCanceledException)
                {
                    // Handle cancellation
                    OnDownloadCompleted(new DownloadeCompletedEventArgs { IsCancelled = true, FilePath = filePath });
                }
                catch (Exception ex)
                {
                    string exx = ex.ToString();
                    OnDownloadCompleted(new DownloadeCompletedEventArgs { IsSuccess = false, FilePath = filePath });
                }
            }

            protected virtual void OnDownloadCompleted(DownloadeCompletedEventArgs e)
            {
                DownloadCompleted?.Invoke(this, e);
            }

            protected virtual void OnDownloadProgress(DownloadProgressEventArgs e)
            {
                DownloadProgress?.Invoke(this, e);
            }
        }
    }
}

  • 2

    What kind of project file do you have? If it’s an SDK-style project, you can use <Nullable>enable</Nullable> in the project properties – but be aware that the .NET Framework itself won’t have been annotated for nullability.

    – 

  • @JonSkeet it’s .NET Framework 4.8

    – 

  • That doesn’t say what kind of project it is…

    – 

  • It doesn’t matter, Framework uses C# 7.3 which doesn’t support it regardless of project type.

    – 

Nullable reference types are a feature added in C# 8 (.NET Core 3). It is possible to force a newer language version in an SDK project..

<PropertyGroup>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
</PropertyGroup>

However, this can cause complications and should be done with caution. This also does not get you every possible feature in higher C#/.Net Versions. It only allows you to use language features that the compiler can work with, as such the compiler used to compile it must also support the language version you specify.

Leave a Comment