[{"id":"csharpadvanced-001","courseId":"csharp-advanced","domain":"language-basics","topic":"ref struct types","prompt":"Sink is a ref struct that implements IWriter. Judge this statement: Log(new Sink()) compiles, because a ref struct converts implicitly to any interface it implements.","hint":"Consider what converting a value to an interface type involves at run time.","explain":"A ref struct is confined to the stack so it can't outlive its scope. Implementing an interface adds restrictions on how the ref struct can be used as that interface, and the compiler enforces them.","source":"https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"The compiler blocks this conversion: it would be a boxing conversion, which violates ref safety."},{"id":"b","text":"False","why":"A ref struct can't be converted to an interface it implements, even implicitly as an argument, so Log(new Sink()) doesn't compile."}],"answer":"b","explanation":"A ref struct can implement interfaces, but the compiler forbids converting it to one of them, including implicitly as an argument, because that conversion would box it.","concept":"A ref struct can implement interfaces, but the compiler forbids converting it to one of them, including implicitly as an argument, because that conversion would box it.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"public interface IWriter\n{\n    void Write(string text);\n}\n\npublic ref struct Sink : IWriter\n{\n    public void Write(string text) { }\n}\n\nvoid Log(IWriter w) => w.Write(\"hi\");\n\nLog(new Sink());","takeaways":["A ref struct that implements an interface still can't be passed where that interface type is expected."]},{"id":"csharpadvanced-002","courseId":"csharp-advanced","domain":"language-basics","topic":"ref locals and ref returns","prompt":"A teammate writes the code below. Which outcome is correct when it is compiled?","hint":"Separate changing a reference's value from changing which element it refers to.","explain":"A reference local is an alias for storage owned by another variable. The ref assignment operator changes which storage it aliases, and a ref readonly local adds a limit on writing through the alias.","source":"https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations","interaction":{"type":"single"},"options":[{"id":"a","text":"The line element = 100 fails to compile, but element = ref xs[^1] is allowed.","why":"A ref readonly local can't be given a value with =, but it can still be ref reassigned to another element."},{"id":"b","text":"The line element = ref xs[^1] fails, because a ref readonly local can never be reassigned.","whyNot":"Ref reassignment is allowed for ref readonly locals; only assigning a value through them is blocked."},{"id":"c","text":"Both lines compile, and xs[2] ends up holding 100.","whyNot":"Assigning 100 through a ref readonly local is a compile error, so no element receives 100."},{"id":"d","text":"Both lines compile, and xs[0] ends up holding 100.","whyNot":"The local is re-pointed to xs[^1] first, and value assignment through a ref readonly local isn't allowed at all."}],"answer":"a","explanation":"A ref readonly local can't receive a value with =, but it can still be ref reassigned with = ref to alias a different element.","concept":"A ref readonly local can't receive a value with =, but it can still be ref reassigned with = ref to alias a different element.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"int[] xs = [1, 2, 3];\nref readonly int element = ref xs[0];\nelement = ref xs[^1];\nelement = 100;","takeaways":["ref readonly limits writes through the alias; it doesn't stop = ref from re-pointing the local."]},{"id":"csharpadvanced-003","courseId":"csharp-advanced","domain":"language-basics","topic":"readonly struct","prompt":"A teammate declares public readonly struct Cart with a List<int> Items field. Which TWO statements about this struct are correct?","hint":"Ask whether the modifier covers the struct's own state or the objects its fields refer to.","explain":"The readonly modifier on a struct limits how its own data can change. It can apply to the whole struct, to individual members, or to both.","source":"https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/struct","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Every field of the struct must be declared readonly.","why":"Any field declaration in a readonly struct must carry the readonly modifier."},{"id":"b","text":"Code holding a Cart can still call Add on its Items list to add elements.","why":"The struct can't replace the List<T> instance, but the list itself can still gain new elements."},{"id":"c","text":"A property of the struct can have a private setter, as long as the struct is readonly.","whyNot":"Every property of a readonly struct must be read-only or init-only, so a private setter isn't allowed."},{"id":"d","text":"A readonly method that calls a non-readonly method changes the original struct's fields.","whyNot":"The compiler runs the non-readonly member on a copy, so the original instance is left unchanged."},{"id":"e","text":"A readonly instance method can assign new values to the struct's instance fields.","whyNot":"A readonly member can't assign to instance fields; that restriction is the point of the modifier."},{"id":"f","text":"The Items list can't change at all, because a readonly struct makes everything it holds immutable.","whyNot":"The struct's own state is fixed, but a mutable reference type inside it still changes its own contents."}],"answer":"ab","explanation":"A readonly struct requires its fields and properties to be read-only; a mutable reference-type field can still change its own contents.","concept":"A readonly struct requires its fields and properties to be read-only; a mutable reference-type field can still change its own contents.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["A readonly struct stops its own state from changing, but not the contents of a reference field it holds."]},{"id":"csharpadvanced-004","courseId":"csharp-advanced","domain":"language-basics","topic":"List patterns","prompt":"A teammate needs a pattern where each element of a sequence is tested by its own nested pattern. Which group contains only recursive pattern kinds?","hint":"Think about which patterns are built out of other patterns inside them.","explain":"Patterns can be combined with and, or and not, and some kinds can contain other patterns inside them. Whether a kind can nest patterns depends on what it tests.","source":"https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns","interaction":{"type":"single"},"options":[{"id":"a","text":"Logical, property, positional, and list patterns","why":"These four kinds can contain nested patterns, so each can test parts of a value against further patterns."},{"id":"b","text":"Declaration, type, and constant patterns","whyNot":"These check a single value's type or compare it to a constant; they don't hold nested patterns."},{"id":"c","text":"Relational and discard patterns","whyNot":"A relational pattern compares to a constant and a discard matches anything; neither contains nested patterns."},{"id":"d","text":"Every pattern kind, including var","whyNot":"The var pattern matches any value and assigns it to a variable, so it isn't a recursive pattern."}],"answer":"a","explanation":"Logical, property, positional and list patterns are recursive: each can contain nested patterns that test parts of the value.","concept":"Logical, property, positional and list patterns are recursive: each can contain nested patterns that test parts of the value.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["List patterns test each element against a nested pattern, which makes them recursive."]},{"id":"csharpadvanced-005","courseId":"csharp-advanced","domain":"language-basics","topic":"Null-forgiving operator","prompt":"LookUpName returns string?, so the compiler flags a possible null dereference on maybeName. What does the ! in maybeName!.Length do?","hint":"The warning is about the declared type; the operator is a promise you make at one use.","explain":"Nullable reference types let the compiler track whether a value might be null, and it warns when a maybe-null value is dereferenced. Code can override that analysis at a single use.","source":"https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/null-safety/nullable-reference-types","interaction":{"type":"single"},"options":[{"id":"a","text":"It tells the compiler to treat maybeName as not-null at that point, so the compiler stops checking that use.","why":"The operator declares the expression not-null even though the analysis says otherwise, so the compiler can no longer protect that use."},{"id":"b","text":"It checks the value at run time and throws if maybeName is null.","whyNot":"The operator isn't a run-time check; it only overrides the compiler's static analysis, so a null would still fail at the dereference."},{"id":"c","text":"It changes the declared type of maybeName to string for the rest of the method.","whyNot":"The declared type stays string?; the operator overrides the analysis at the place it's written, not the type."},{"id":"d","text":"It changes LookUpName so that it can no longer return null.","whyNot":"The return type is still string?, because the null arm is still there; the operator doesn't touch the method."}],"answer":"a","explanation":"The null-forgiving operator ! overrides the compiler's null-state analysis for one expression, so the compiler stops protecting that use.","concept":"The null-forgiving operator ! overrides the compiler's null-state analysis for one expression, so the compiler stops protecting that use.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"string? LookUpName(string id) => id switch\n{\n    \"ada\" => \"Ada Lovelace\",\n    _ => null,\n};\n\nstring? maybeName = LookUpName(\"ada\");\nint length = maybeName!.Length;","takeaways":["Use ! sparingly: each one is a spot where the compiler can no longer catch a null."]}]