= CSharp =
CSharp is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, procedural, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within its .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006). C# is one of the programming languages designed for the Common Language Infrastructure. From [[http://en.wikipedia.org/wiki/C_Sharp_(programming_language)|Wikipedia]]

== Compile code based on csc (CSharp compiler) ==
Look for the compiler in '''c:\windows\Microsoft.NET\Framework\<version>\csc.exe'''

Hello world sample code:
{{{#!highlight csharp
using System;

namespace org.bitarus.allowed
{
  public class Program
  {
    public static void Main(string[] args)
    {
      System.Console.WriteLine("Hello world");
    }
  }
}

}}}

Compile HelloWorld.cs:
 * csc /t:exe /out:HelloWorld.exe HelloWorld.cs

== Build batch file example .NET 4.0 ==
{{{
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:library /out:libx.dll src\_Default.cs src\DALSql.cs src\Global.cs
}}}

== app.config sample ==
{{{#!highlight xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="keyx" value="valuex"/>
  </appSettings>
</configuration>
}}}

{{{#!highlight csharp
using System.Configuration;
//.....
string valuex = ConfigurationManager.AppSettings["keyx"];
System.Console.WriteLine(valuex);
}}}


Copy src/app.config to AppX.exe.config.

== JSON library several .net version ==
http://james.newtonking.com/json

=== Deserialization example ===
{{{#!highlight csharp
// mcs test.cs /r:Newtonsoft.Json.dll  
// mono test.exe 

using Newtonsoft.Json;
using System;

public class Reply{
  public int mnr {get;set;}
  public int fnr {get;set;}
  public MSG[] msg {get;set;}
}

public class MSG{
  public DateX date {get;set;}
  public string msg {get;set;}

}

public class DateX{
  [JsonProperty("$date")]
  public long datex { get;set; }  
}

public class Program{
  public static void Main(string[] args){
    string json = @"{\"mnr\": 1, \"fnr\": 777, \"msg\": [
{\"date\": {\"$date\": 1388679281000}, \"msg\": \"AAAAA\"}, 
{\"date\": {\"$date\": 1388678779000}, \"msg\": \"BBBBB\"}, 
{\"date\": {\"$date\": 1388676003000}, \"msg\": \"CCCCC\"}
]}";

    Reply r = JsonConvert.DeserializeObject<Reply>(json);
    System.Console.WriteLine(String.Format("Fnumber:{0} MNumber:{1}",r.fnr,r.mnr));

    foreach(MSG msg in r.msg) {
      System.Console.WriteLine(String.Format("MSG:{0} Date:{1} ",msg.msg, msg.date.datex ));
    }      
  }
}
}}}

== String format examples ==
{{{#!highlight csharp

using System;

public class Test{
  public static void Main(String[] args)  {
    int a=123;
    System.Console.WriteLine( String.Format("{0}",a) ); // 123
    System.Console.WriteLine( String.Format("{0:D2}",a) ); //123
    System.Console.WriteLine( String.Format("{0:D5}",a) ); // 00123
  }			  
}

}}}

== IL sample code ==
IL code from C# build with Mono.

{{{#!highlight csharp
/*
Compile code
mcs -sdk:4.5 testeILAsm.cs 

Get IL code
monodis testeILAsm.exe > testeILAsm.il
*/

using System;
using System.IO;

namespace org.bitarus.allowed
{
  public class Program
  {
    private static void dummy(int a,int b)    {
      int res=sum(a,b);
      log(a,b);
      log(a,b);
    }
    
    private static void log(int skey,int salt)    {
      string line=String.Format("skey:{0:X8} Salt:{1:X8}",skey,salt);
      
      using (StreamWriter w =File.AppendText("logx.txt"))	{
	w.WriteLine(line);	  
      }
      
      System.Console.WriteLine(line);
    }

    private static int sum(int a,int b)
    {
      return a+b;
    }
    
    public static void Main(string[] args)
    {
      System.Console.WriteLine("Hello world");      
      int x1=1234;
      int x2=5678;
      dummy(x1,x2);
    }
  }
}

}}}

{{{#!highlight
.assembly extern mscorlib
{
  .ver 4:0:0:0
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
}
.assembly 'testeILAsm'
{
  .custom instance void class [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::'.ctor'() =  (
		01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
		63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01       ) // ceptionThrows.

  .hash algorithm 0x00008004
  .ver  0:0:0:0
}
.module testeILAsm.exe // GUID = {6FFBF541-A6EC-4419-8CA6-549A5A6E2EA7}


.namespace org.bitarus.allowed
{
  .class public auto ansi beforefieldinit Program
  	extends [mscorlib]System.Object
  {

    // method line 1
    .method private static hidebysig 
           default void dummy (int32 a, int32 b)  cil managed 
    {
        // Method begins at RVA 0x2050
	// Code size 23 (0x17)
	.maxstack 2
	.locals init (
		int32	V_0)
	IL_0000:  ldarg.0 
	IL_0001:  ldarg.1 
	IL_0002:  call int32 class org.bitarus.allowed.Program::sum(int32, int32)
	IL_0007:  stloc.0 
	IL_0008:  ldarg.0 
	IL_0009:  ldarg.1 
	IL_000a:  call void class org.bitarus.allowed.Program::log(int32, int32)
	IL_000f:  ldarg.0 
	IL_0010:  ldarg.1 
	IL_0011:  call void class org.bitarus.allowed.Program::log(int32, int32)
	IL_0016:  ret 
    } // end of method Program::dummy

    // method line 2
    .method private static hidebysig 
           default void log (int32 skey, int32 salt)  cil managed 
    {
        // Method begins at RVA 0x2074
	// Code size 66 (0x42)
	.maxstack 3
	.locals init (
		string	V_0,
		class [mscorlib]System.IO.StreamWriter	V_1)
	IL_0000:  ldstr "skey:{0:X8} Salt:{1:X8}"
	IL_0005:  ldarg.0 
	IL_0006:  box [mscorlib]System.Int32
	IL_000b:  ldarg.1 
	IL_000c:  box [mscorlib]System.Int32
	IL_0011:  call string string::Format(string, object, object)
	IL_0016:  stloc.0 
	IL_0017:  ldstr "logx.txt"
	IL_001c:  call class [mscorlib]System.IO.StreamWriter class [mscorlib]System.IO.File::AppendText(string)
	IL_0021:  stloc.1 
	.try { // 0
	  IL_0022:  ldloc.1 
	  IL_0023:  ldloc.0 
	  IL_0024:  callvirt instance void class [mscorlib]System.IO.TextWriter::WriteLine(string)
	  IL_0029:  leave IL_003b

	} // end .try 0
	finally  { // 0
	  IL_002e:  ldloc.1 
	  IL_002f:  brfalse IL_003a

	  IL_0034:  ldloc.1 
	  IL_0035:  callvirt instance void class [mscorlib]System.IDisposable::Dispose()
	  IL_003a:  endfinally 
	} // end handler 0
	IL_003b:  ldloc.0 
	IL_003c:  call void class [mscorlib]System.Console::WriteLine(string)
	IL_0041:  ret 
    } // end of method Program::log

    // method line 3
    .method private static hidebysig 
           default int32 sum (int32 a, int32 b)  cil managed 
    {
        // Method begins at RVA 0x20d4
	// Code size 4 (0x4)
	.maxstack 8
	IL_0000:  ldarg.0 
	IL_0001:  ldarg.1 
	IL_0002:  add 
	IL_0003:  ret 
    } // end of method Program::sum

    // method line 4
    .method public static hidebysig 
           default void Main (string[] args)  cil managed 
    {
        // Method begins at RVA 0x20dc
	.entrypoint
	// Code size 30 (0x1e)
	.maxstack 2
	.locals init (
		int32	V_0,
		int32	V_1)
	IL_0000:  ldstr "Hello world"
	IL_0005:  call void class [mscorlib]System.Console::WriteLine(string)
	IL_000a:  ldc.i4 1234
	IL_000f:  stloc.0 
	IL_0010:  ldc.i4 5678
	IL_0015:  stloc.1 
	IL_0016:  ldloc.0 
	IL_0017:  ldloc.1 
	IL_0018:  call void class org.bitarus.allowed.Program::dummy(int32, int32)
	IL_001d:  ret 
    } // end of method Program::Main

    // method line 5
    .method public hidebysig specialname rtspecialname 
           instance default void '.ctor' ()  cil managed 
    {
        // Method begins at RVA 0x2106
	// Code size 7 (0x7)
	.maxstack 8
	IL_0000:  ldarg.0 
	IL_0001:  call instance void object::'.ctor'()
	IL_0006:  ret 
    } // end of method Program::.ctor

  } // end of class org.bitarus.allowed.Program
}
}}}

== CIL instructions ==
Adapted from http://en.wikipedia.org/wiki/List_of_CIL_instructions

|| Opcode || Instruction || Description ||
|| 0x00 || nop || do nothing ||
|| 0x2A || ret || return from method possibly with value ||
|| 0x28	|| call <method> || Call method described by method ||
|| 0x72	|| ldstr <string> || Push a string object for the literal string ||
|| 0x02	|| ldarg.0 || Load argument 0 onto the stack ||
|| 0x03	|| ldarg.1 || Load argument 1 onto the stack ||
|| 0x16	|| ldc.i4.0 || Push 0 onto the stack as int32 ||
|| 0x17	|| ldc.i4.1 || Push 1 onto the stack as int32 ||
|| 0x20	|| ldc.i4 <int32 (num)> || Push num of type int32 onto the stack as int32 ||
|| 0x0A	|| stloc.0 || Pop a value from stack into local variable 0  ||
|| 0x0B	|| stloc.1 || Pop a value from stack into local variable 1  ||
|| 0x06	|| ldloc.0 || Load local variable 0 onto stack  ||
|| 0x07	|| ldloc.1 || Load local variable 1 onto stack  ||

IL code usually resides before string 'BSJB' 0x45534A42.