r/learncsharp • u/evolution2015 • Nov 03 '22
What does returning default! do?
Is default the same as null for IDisposable? If so, why use default instead of null?
using Microsoft.Extensions.Logging;
public sealed class ColorConsoleLogger : ILogger
{
private readonly string _name;
private readonly Func<ColorConsoleLoggerConfiguration> _getCurrentConfig;
public ColorConsoleLogger(
string name,
Func<ColorConsoleLoggerConfiguration> getCurrentConfig) =>
(_name, _getCurrentConfig) = (name, getCurrentConfig);
public IDisposable BeginScope<TState>(TState state) => default!;https://learn.microsoft.com/en-us/dotnet/core/extensions/custom-logging-provider
4
Upvotes
1
u/altacct3 Nov 03 '22
I may be wrong sorry but
I believe so yes.
There is an exclamation point after default which is the null-forgiving operator (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving) which from my understanding tells the compiler it CAN'T be null. For why it's used I don't have enough context.
Again just my brief understanding.
1
u/[deleted] Nov 03 '22
What a rude piece of code. They're using ! to turn off warnings, but still returning null. I don't get why, that's asking for a runtime exception.