= 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\\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: {{{#!highlight sh 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 }}} {{{#!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(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 (Intermediate Language) 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 || Call method described by method || || 0x72 || ldstr || 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 || 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. == Simple TCP server == {{{#!highlight csharp using System; using System.Net; using System.Net.Sockets; using System.Threading; using System.Text; public class Server { public static void ConnectionHandler(object socket) { if( socket is Socket ) { var s=(Socket)socket; bool exit=false; while(!exit) { byte[] rxbuffer = new byte[8192]; int nrRx = s.Receive(rxbuffer,rxbuffer.Length,0); string rx = Encoding.ASCII.GetString(rxbuffer, 0, nrRx); System.Console.Write(rx.ToUpper()); if(rx=="exit\n") { exit=true; } Byte[] tx = Encoding.ASCII.GetBytes(rx.ToUpper()); s.Send(tx,tx.Length,0); } s.Close(); } } public static void Main(string[] args) { Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint( new IPAddress(new byte[]{0,0,0,0 }) ,9090); var handlerCallback = new WaitCallback(ConnectionHandler); s.Bind(ep); s.Listen(0); while(true) { Socket rx = s.Accept(); ThreadPool.QueueUserWorkItem(handlerCallback, rx ); } } } }}} == Sample Web Service with Mono + IIS 6 == web.config {{{#!highlight xml }}} ws.asmx {{{#!highlight xml <%@ WebService Class="WS.RemoteWebService" %> }}} ws.cs {{{#!highlight csharp //gmcs /r:System,System.Web,System.Web.Services ws.cs /t:library using System; using System.Web; using System.Web.Services; namespace WS { [WebService (Description="Our first web service")] public class RemoteWebService : System.Web.Services.WebService { [WebMethod (Description="Adds two numbers")] public int Add (int firstNumber, int secondNumber) { return firstNumber + secondNumber; } } } }}} Deploy the DLL inside a bin folder == Linq example == {{{#!highlight csharp using System; using System.Collections.Generic; using System.Linq; interface Filter{ List filterNumbers(List numbers); } class NormalFilter:Filter{ public List filterNumbers(List numbers){ var oddNumbers = new List(); foreach(var nr in numbers){ if( nr%2==0){ oddNumbers.Add(nr); } } return oddNumbers; } } class LinqFilter:Filter{ public List filterNumbers(List numbers){ var oddNumbersLinq = numbers.Where( x => x%2==0).ToList(); return oddNumbersLinq; } } class MainClass { public static void Main (string[] args) { var numbers = new List(); for(int i=1;i<=10;i++){ numbers.Add(i); } var oddNumbers = new List(); foreach(var nr in numbers){ if( nr%2==0){ oddNumbers.Add(nr); } } var oddNumbersLinq = numbers.Where( x => x%2==0).ToList(); var fromNormal = new NormalFilter().filterNumbers(numbers); var fromLinq = new LinqFilter().filterNumbers(numbers); fromNormal.ForEach(x => Console.WriteLine(x) ); fromLinq.ForEach(x => Console.WriteLine(x) ); } } }}} Other implementation {{{#!highlight csharp using System; using System.Collections.Generic; using System.Linq; namespace Even{ public interface IFilter{ IEnumerable FilterEven(IList data); } public class NormalLoop : IFilter { public IEnumerable FilterEven(IList data){ List ret = new List(); for(var i=0; i FilterEven(IList data){ return data.Where(nr => nr % 2 == 0); } } public class MainClass { public static void Main (string[] args) { int[] i={1,2,3,4,5,6,7,8,9,10}; IFilter loop = new NormalLoop(); IFilter loopLinq= new LinqLoop(); Console.WriteLine("Normal impl"); foreach(var item in loop.FilterEven(i) ){ Console.WriteLine(item); } Console.WriteLine("Linq impl"); foreach(var item in loopLinq.FilterEven(i) ){ Console.WriteLine(item); } } } } }}}