C
C#17mo ago
ogie1985

❔ Difference string and string?

string? t = null; // no warning string t2 = null; // warning Console.WriteLine(t); Console.WriteLine(t2); //behaves same
10 Replies
Angius
Angius17mo ago
? denotes nullability
ogie1985
ogie198517mo ago
Yeah but looks like t2 is also null ?
Angius
Angius17mo ago
Well, it's... tricky Prepare for a story lol
ogie1985
ogie198517mo ago
I knew it was comming :p
Angius
Angius17mo ago
Used to be, that all reference types (types based on classes) were nullable by default. Meaning, if you had a class Foo {} you could do Foo f = null without any issue. Value types meanwhile (types based on structs) were not nullable by default, so the same would throw an exception All primitive types (int, bool, float, etc) in C# are implemented as value types Except string That's because value types are stored differently in memory than reference types. That has some benefits, but one drawback is limited size. And since strings can get quite large, it made sense to implement them as reference types, but provide a lot of value-type-like semantics. Like value equality instead of reference equality One thing that was left for reasons unbeknownst to me, is that string was nullable by default, like the rest of reference tyopes Cue .NET 6 and nullable reference types That made it so, by default, the compiler will warn you about trying to assign null to reference types As opposed to actual value types, though, they decided to not make it a full-on exception, for backwards compatibility reasons And there isn't really anything that stops you from treating reference types as nullable by default in the first place. Nothing really changed for them, except the warnings being added So... that makes it so that despite string being nullable under the hood, since it's a reference type, you get a warning because of nullable reference types that came in .NET 6 It's a weird limbo. Reference are kinda-nullable-but-kinda-not Now, you can make such warnings about nullability turn into errors, if you want to be strict. By adding <WarningsAsErrors>Nullable</WarningsAsErrors> to PropertyGroup in your .csproj file
ogie1985
ogie198517mo ago
Interesting, I know strings are some special kind in C#. Not rly new to C# but since these warnings came with nullable stuff I got confused lately on wheter to use or not use nullable types. it made no sense to me that if it behaves the same that it gives a warning But I guess string? > string if it can be nullable yh?
Angius
Angius17mo ago
Yeah It compiles to Nullable<string>
ogie1985
ogie198517mo ago
Cool, cool
TheBoxyBear
TheBoxyBear17mo ago
Only value types get wrapped into Nullable<T> which is a different type that spoofs null checks and will prevent compilation if you do say int a = (int?)1 Reference types get labeled with an attribute and only lead to warnings though it still compile as the actual type is the same Both use the question mark suffix but are two different concepts. Spoofs the value as in Nullable is a struct so it can't be null. It has an internal flag for if the value is meant to be null and comparisons are based on that. If it were actually nullable, the HasValue property couldn't work
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Blazor high frequency UI changesWhen working with Blazor. I have a table that contains data and control components. These rows have ✅ Cannot implicit convert typeI am working on this LC puzzle https://leetcode.com/problems/binary-tree-level-order-traversal/?envT✅ intercept keyboardI'm working on a kiosk app that should lock out certain user levels, just inquiring the best method ❔ Not sure what to use to Code (Idk language)I want to make something but I'm not sure how I would, it's not about learning how to code. It's abo❔ Using a Custom JSON Converter on NswagStudio's Generated C# ClientDoes anyone know how I can make NswagStudio use a custom json converter that I made on the generated❔ C# Fundamental: does setting one object to another point or copy?```cs public SomeType someObject = new SomeType(); public SomeType anotherObject = someObject; ``` ❔ [RazorPages] Any clues why this Select isn't working?```csharp Index.cshtml.cs public async Task<IActionResult> OnGet(string qstring) { Console.writeli✅ Adding functionality to a call of duty mw2 (2009) steam toolHello, I have a tool which creates clean mw2 lobbies (free from mods etc). It has a kick feature whi❔ Practical use for bitwise operators, specifically |= in context.**Not bitwise application. This is a simple boolean or equals** Howdy folks. Currently working in a❔ I want to pass information from Form3 to listBox in Form2Hello, I want to transfer the information entered in the textBox in Form3 to the listBox in Form2 wh