About optional parameters in c# when targeting .net 2.0

One of the coolest thing about Visual Studio 2010 is C# 4.0 and its optional/named parameters feature. I was wondering if I can get rid of those hard to maintain overloads in my older assemblies which still need to target the CLR2.

Just to be sure I made a quick proof of concept:

Code for test program

   1: using System;
   2:  
   3: namespace OptionalParameterTest
   4: {
   5:     class Program
   6:     {
   7:         static void Main(string[] args)
   8:         {
   9:             TestMethod();
  10:             TestMethod("only string set");
  11:             TestMethod("both set", 12345);
  12:             TestMethod(someint: 12345, someString: "named parameters, both set");
  13:             TestMethod(someString: "named parameters, only string set");
  14:         }
  15:  
  16:         private static void TestMethod(String someString = "defaultStringValue", int someint = 9)
  17:         {
  18:             Console.WriteLine("Called with: " + someString + " " + someint);
  19:         }
  20:     }
  21: }

This is ofc expected to result with such output:

   1: C:\WM\Varia\OptionalParameterTest\bin\Debug>OptionalParameterTest.exe
   2: Called with: defaultStringValue 9
   3: Called with: only string set 9
   4: Called with: both set 12345
   5: Called with: named parameters, both set 12345
   6: Called with: named parameters, only string set 9

So, the target FW is 4.0

Compile, run, works as expected. Corflags agree that the executable indeed requires version 4:

   1: Version   : v4.0.30319
   2: CLR Header: 2.5
   3: PE        : PE32
   4: CorFlags  : 3
   5: ILONLY    : 1
   6: 32BIT     : 1
   7: Signed    : 0

Lets note that we have such line in app.config added by VS itself:

   1: <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>

When target FW is 2.0

Changed target fw of the VS project, compile, run.. result was as expected and corflags agrees that we now require older CLR:

   1: Version   : v2.0.50727
   2: CLR Header: 2.5
   3: PE        : PE32
   4: CorFlags  : 3
   5: ILONLY    : 1
   6: 32BIT     : 1
   7: Signed    : 0

Btw, VS automatically adjusted my app.config to require correct version as shown below. Nice touch!

   1: <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

But lets be paranoid..

.. and really make sure that the correct runtime version corflags reports was used when running the application. For this I deployed both solutions to an box without .NET 4, just .NET 3.5.

  • Assembly targeted against CLR2.0 ran just fine. (This is what makes me happy!)
  • As expected the assembly targeted against CLR4.0 refused to run stating quite descriptevely that it needed .Net FW 4.0.

Note:

When app.config lies abiout it’s required runtime version, like stating CLR 2.0 instead of 4.0 then you get much uglier and much less descriptive “crash, debug?”-message. Be careful about that and do use the supported runtime  version setting in configuration file instead of just assuming there will be the right one present and used.

Conclusion

Optional and named parameters are C# compiler-level features. Which means the runtime does not care if you use them or not – compiler is just better at understanding which method’s call to use and inserts parameter initialization where neccessary. The generated MSIL is still CLR 2.0. The compiler just helps us to achieve more by writing less. One more reason to switch to VS2010 even when still targeting older frameworks for whatever reasons. Yei!

Sellest, et CSV jaoks on Excel saamatu..

Minu jaoks täiesti ootamatult on MS Excel 2007 andmete CSV-formaati konvertimine puudulik. Probleemideks on:

  • csv eksport ei toeta unicode’i.
    • Ma isegi ei tea, mis kodeeringus ta faili väljastab. Ühtegi seadistust ma küll ei näinud ei salvestamise ajal ega optionite all.
    • salvestamise on ka failitüübi valik “Unicode text”, mis VIST on Tab-separated CSV.. aga mida see täpselt teeb, ei tea. Ei viitsinud testida tekstisiseid reavahetusi. tabulaatorimärke ja mähkimise tähtede korral käitumist..
  • csv eraldajat ega väärtuste mähkimise tähte ei saa ise määrata.
    • Küll aga on mitu erinevad CSV analoogi save-dialoogi all failitüüpide loetelus, näiteks “CSV (Comma delimited)” jms suvalises järjestuses 28 võimaliku “failitüübi” seas.

Selgus, et OpenOffice Calc 3.2 seevastu küsib viisakalt CSV faili salvestamisel kõiki kolme asja:

  • Character set – sh unicode
  • Field delimiter
  • Text delimiter
  • .. muid asju küsib ka aga neist ma praegu ei hoolinud.

Täpselt see, mis vaja ja täpselt seal kus vaja. Hea MS, vaata ja õpi, milline peab olema CSV ekspordi dialoog!