Sep 30, 2022

A small C# 10 programming puzzle.

Leia este post em português

Lire cet post en Français.

Are you a developer using C# in a day to day basis? Are you following the latest development, mainly in the C# 10 version? Are you interested in puzzles? 

If so, given the following program, can you, changing only the  type of the parameter msg in Foo() method,  change the output of the program to print only the lines containing even numbers?

I.e printing:

instead of:

Summarizing, the rules are:

  1. You cannot change Foo's  implementation in any way other than changing its msg parameter type.
  2. You cannot change the implementation of the for loop in any ways.
  3. You cannot rely on IL post-processing in any way (but it would be a nice exercise 😃)
  4. Any other changes to the source are acceptable

Some tips, in case you don't know the answer yet:

  1. The solution for the proposed challenge (at least the one I am interested) depends on a C# feature that has been improved in version 10
  2. Inspect the source with attention and look for suspicious code, they do contain some clues on how to achieve your goal.

In the next posts I'll show, and discuss, my solution to that. I am curious to see what other solutions exists.

Have fun!

Adriano

No comments: