Last active
November 23, 2025 03:34
-
-
Save GeeLaw/e442311dcc1503979988db927b0735f4 to your computer and use it in GitHub Desktop.
C# evaluation: oh what fun, it is to throw!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| See | |
| https://github.com/dotnet/csharplang/discussions/9836 | |
| Copyright (c) 2025 Ji Luo | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| */ | |
| using System; | |
| using System.Runtime.CompilerServices; | |
| public class ReferenceType | |
| { | |
| public int Field; | |
| public int SettableProperty { set { } } | |
| public ref int ByRefProperty { get { return ref Field; } } | |
| public int this[int index] { set { } } | |
| public ref int this[int a, int b] { get { return ref Field; } } | |
| } | |
| internal class Program | |
| { | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static ReferenceType GetInstance() => null; | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static int[] GetArray() => null; | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static int[,] GetArray2D() => null; | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static int Throw(string msg) | |
| { | |
| Console.WriteLine(msg + " throw"); | |
| throw new ApplicationException(); | |
| } | |
| [MethodImpl(MethodImplOptions.NoInlining)] | |
| static int Proceed(string msg) | |
| { | |
| Console.WriteLine(msg); | |
| return 0; | |
| } | |
| static void PrintBanner(string which) | |
| { | |
| Console.WriteLine(); | |
| Console.WriteLine("testing " + which); | |
| } | |
| static void TestField(ReferenceType o) | |
| { | |
| PrintBanner("null.Field = throw;"); | |
| try | |
| { o.Field = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null.Field = value;"); | |
| try | |
| { o.Field = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestSettableProperty(ReferenceType o) | |
| { | |
| PrintBanner("null.SettableProperty = throw;"); | |
| try | |
| { o.SettableProperty = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null.SettableProperty = value;"); | |
| try | |
| { o.SettableProperty = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestByRefProperty(ReferenceType o) | |
| { | |
| PrintBanner("null.ByRefProperty = throw;"); | |
| try | |
| { o.ByRefProperty = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null.ByRefProperty = value;"); | |
| try | |
| { o.ByRefProperty = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestSettableIndexer(ReferenceType o) | |
| { | |
| PrintBanner("null[throw] = throw; (settable indexer)"); | |
| try | |
| { o[Throw("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[throw] = value; (settable indexer)"); | |
| try | |
| { o[Throw("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value] = throw; (settable indexer)"); | |
| try | |
| { o[Proceed("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value] = value; (settable indexer)"); | |
| try | |
| { o[Proceed("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestByRefIndexer(ReferenceType o) | |
| { | |
| PrintBanner("null[value, throw] = throw; (byref indexer)"); | |
| try | |
| { o[0, Throw("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, throw] = value; (byref indexer)"); | |
| try | |
| { o[0, Throw("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, value] = throw; (byref indexer)"); | |
| try | |
| { o[0, Proceed("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, value] = value; (byref indexer)"); | |
| try | |
| { o[0, Proceed("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestArrayIndexer(int[] a) | |
| { | |
| PrintBanner("null[throw] = throw; (array indexer)"); | |
| try | |
| { a[Throw("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[throw] = value; (array indexer)"); | |
| try | |
| { a[Throw("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value] = throw; (array indexer)"); | |
| try | |
| { a[Proceed("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value] = value; (array indexer)"); | |
| try | |
| { a[Proceed("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void TestArrayIndexer2D(int[,] a) | |
| { | |
| PrintBanner("null[value, throw] = throw; (md-array indexer)"); | |
| try | |
| { a[0, Throw("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, throw] = value; (md-array indexer)"); | |
| try | |
| { a[0, Throw("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, value] = throw; (md-array indexer)"); | |
| try | |
| { a[0, Proceed("index")] = Throw("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| PrintBanner("null[value, value] = value; (md-array indexer)"); | |
| try | |
| { a[0, Proceed("index")] = Proceed("RHS"); } | |
| catch (Exception e) { Console.WriteLine("caught " + e.GetType().Name); } | |
| } | |
| static void Main(string[] args) | |
| { | |
| var o = GetInstance(); | |
| TestField(o); | |
| TestSettableProperty(o); | |
| TestByRefProperty(o); | |
| TestSettableIndexer(o); | |
| TestByRefIndexer(o); | |
| var a = GetArray(); | |
| TestArrayIndexer(a); | |
| var a2 = GetArray2D(); | |
| TestArrayIndexer2D(a2); | |
| } | |
| } | |
| /* | |
| .NET Framework 2.0 on Windows 11 (version 24H2), | |
| .NET 10.0 on Windows 11 (version 24H2): | |
| testing null.Field = throw; | |
| RHS throw | |
| caught ApplicationException | |
| testing null.Field = value; | |
| RHS | |
| caught NullReferenceException | |
| testing null.SettableProperty = throw; | |
| RHS throw | |
| caught ApplicationException | |
| testing null.SettableProperty = value; | |
| RHS | |
| caught NullReferenceException | |
| testing null.ByRefProperty = throw; | |
| caught NullReferenceException | |
| testing null.ByRefProperty = value; | |
| caught NullReferenceException | |
| testing null[throw] = throw; (settable indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[throw] = value; (settable indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value] = throw; (settable indexer) | |
| index | |
| RHS throw | |
| caught ApplicationException | |
| testing null[value] = value; (settable indexer) | |
| index | |
| RHS | |
| caught NullReferenceException | |
| testing null[value, throw] = throw; (byref indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, throw] = value; (byref indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, value] = throw; (byref indexer) | |
| index | |
| caught NullReferenceException | |
| testing null[value, value] = value; (byref indexer) | |
| index | |
| caught NullReferenceException | |
| testing null[throw] = throw; (array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[throw] = value; (array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value] = throw; (array indexer) | |
| index | |
| RHS throw | |
| caught ApplicationException | |
| testing null[value] = value; (array indexer) | |
| index | |
| RHS | |
| caught NullReferenceException | |
| */ | |
| /** | |
| .NET Framework 2.0-4.5 x86/x64 (Windows 11 version 24H2) | |
| testing null[value, throw] = throw; (md-array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, throw] = value; (md-array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, value] = throw; (md-array indexer) | |
| index | |
| caught NullReferenceException | |
| testing null[value, value] = value; (md-array indexer) | |
| index | |
| caught NullReferenceException | |
| **/ | |
| /** | |
| .NET 8.0, 10.0 x64 (Windows 11 version 24H2) | |
| !!! Note how "RHS" is appears in the last 2 cases !!! | |
| testing null[value, throw] = throw; (md-array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, throw] = value; (md-array indexer) | |
| index throw | |
| caught ApplicationException | |
| testing null[value, value] = throw; (md-array indexer) | |
| index | |
| RHS throw | |
| caught ApplicationException | |
| testing null[value, value] = value; (md-array indexer) | |
| index | |
| RHS | |
| caught NullReferenceException | |
| **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment