.NET Internals Cookbook Part 0 — Table of contents

This is the table of contents of the .NET Internals Cookbook. Look below for links to all answers.

In this series I answer various .NET questions. Some of them are asked during interviews, some of them I see on the internet, some of them are completely made up. The goal is to provide short answer with links to references if needed. This is by no means a .NET tutorial or experts reference, this is just a bunch of useful answers to refresh your knowledge.

.NET Internals Cookbook is also available as an e-book with additional content — .NET Internals Cookbook on Amazon.com

Already answered

Part 1 — Exceptions, filters and corrupted processes
1. What happens when you throw something which does not inherit from System.Exception? Was it different in .NET 1?
2. How to swallow ThreadAbortException?
3. How to catch AccessViolationException and similar? How was it working in .NET 1?
4. Is it possible that finally block is not executed? Is it possible that only some of them are not executed?
5. What is fault?
6. What is an exception filter? Is it just a syntax sugar?

Part 2 — GC-related things
7. How to compact the LOH?
8. What objects are allocated on the LOH?
9. How to ask the CLR to not throw out-of-band exceptions?
10. How to resurrect an object? How to make it being cleaned up again?
11. What is VM_HOARDING?
12. How to turn the GC off?

Part 3 — Initialization tricks
13. Can a variable be true and false at the same time?
14. Can you create an object without invoking its constructor?
15. Can you invoke object’s constructor multiple times?
16. Can you return null from a constructor?
17. Can you create an instance of an interface?

Part 4 — Type members
18. Can you add type constructor to the interface?
19. How to change the name of the indexer?
20. Can you have static fields and methods in the interface?
21. How many objects are created in new Foo {Bar = 1}?
22. What is covariance? Contravariance? How does it work with arrays and with generics?
23. Can you use extension methods with dynamic?
24. What is the difference between Equals and ==?
25. How to write parameterless constructor for a struct?
26. What is the difference between new Struct and default(Struct)?

Part 5 — Methods, parameters, modifiers
27. Can two methods differ in return type only?
28. What is the difference between const and readonly?
29. How are arguments passed in C#? By value? By reference? Differently?
30. In what order are method parameters calculated?
31. What is the difference between typeof and GetType?
32. What is behind using and lock? Was it always compiled in this way?
33. What is the .NET calling convention?
34. Can you have const decimal? const string? const Foo?
35. Can you pass a volatile variable by reference?

Part 6 — Object internals
36. What is a shim?
37. How many bytes does an empty structure have?
38. What is a sync block?
39. How many app domains are there by default?
40. Does a value type have a method table?
41. What is the base class of the delegate?
42. What is the base class of an array?
43. Can you make a 2-based array?
44. What is a string interning?

Part 7 — Word tearing, locking and others
45. Should we always avoid boxing?
46. What happens if first delegate method throws an exception?
47. Does foreach require interface? What is a WellKnownMember?
48. How does LINQ query syntax work? How is it compiled?
49. What is the difference between Select in IEnumerable and IQueryable?
50. How to block access to private members via reflection?
51. Can you lock a value type?

Part 8 — C# gotchas
52. Can you have class in a class? Class in an interface? Interface in a class? Interface in an interface?
53. How is switch compiled?
54. What is __makeref? __refvalue? __reftype? __arglist?
55. How do you make union in C#?
56. Can x == x ever give false? What about x.Equals(x)?
57. Can you have a global field in C#?
58. Can you have try/catch/finally in method with yield/async?

Part 9 — Finalizers, queues, card tables and other GC stuff
59. What happens if an exception is thrown in finalizer? What about infinite loop?
60. What is finalization queue? What is f-reachable queue?
61. What is object pinning?
62. Does P/Invoke pins objects?
63. What is a write barrier?
64. What is a card table? Brick table?

Part 10 — Threads, Tasks, asynchronous code and others
65. How can you await async void method or catch exceptions thrown in it?
66. Are streams thread safe?
67. What is the difference between Thread.Yield and Thread.Sleep(0)?
68. How many threads are there by default?
69. How big is the thread by default?
70. What happens if an exception is thrown in async Task method but nobody awaits it?
71. Does CLR support fibers?
72. Does Thread.Yield or Thread.Join pump COM messages?
73. How does Thread.Abort works under the hood?
74. What are the memory model rules?

Part 11 — Various C# riddles
75. What is the TimeSpan resolution?
76. What is shared between app domains?
77. Is Timer removed when there is no reference to it but it has callbacks?
78. What is the difference between delegate { ... } and delegate() { ... }?
79. Can you have DllMain in C#?
80. Can you implement your own Nullable< T>?
81. Can you have more visible type inherit from less visible one?

Part 12 — Memory structure, attributes, handles
82. What is a bit-mapped attribute? Custom attribute? Pseudo-custom attribute?
83. What is a handle-recycling attack?
84. What handle types do we have in .NET?
85. What is a ref local?
86. What is an interior pointer?
87. What is a ref struct?
88. What is an unsafe struct?
89. What is a readonly struct?
90. What is an unsafe type?
91. What is a blittable type?
92. How is unmanaged class compiled in C++/CLI?