Skip to main content
deleted 51 characters in body
Source Link
Nick Darvey
  • 460
  • 1
  • 4
  • 5

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42; } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result); var more = result * 42; // "CS8602: Dereference of a potentially null reference" 

Further details:

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42; } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result); var more = result * 42; // "CS8602: Dereference of a potentially null reference" 

Further details:

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42; } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result); var more = result * 42; // "CS8602: Dereference of a potentially null reference" 

Further details:

added 27 characters in body
Source Link
Nick Darvey
  • 460
  • 1
  • 4
  • 5

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 4242; } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result); var more = result * 42; // "CS8602: Dereference of a potentially null reference" 

Further details:

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42 } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result) // "CS8602: Dereference of a potentially null reference" 

Further details:

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42; } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result); var more = result * 42; // "CS8602: Dereference of a potentially null reference" 

Further details:

Source Link
Nick Darvey
  • 460
  • 1
  • 4
  • 5

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics.CodeAnalysis space which you can use for the try pattern.

For example:

how does generic code like Dictionary.TryGetValue deal with this?

bool TryGetValue(TKey key, [MaybeNullWhen(returnValue: false)] out TValue value); 

which means you get yelled at if you don't check for a true

// This is okay: if(myDictionary.TryGetValue("cheese", out var result)) { var more = result * 42 } // But this is not: _ = myDictionary.TryGetValue("cheese", out var result) // "CS8602: Dereference of a potentially null reference" 

Further details: