Difference: UsingCSharpOnPatas (9 vs. 10)

Revision 102008-11-24 - gslayden

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Using C# on Patas

Line: 10 to 10
 
1. Sample Program
   using System;
   using System.Text;

Added:
>
>
  static class MainClass { static void Main(String[] args) { WriteLine ("hello world"); }
Changed:
<
<
}
>
>
}
  To compile and run on patas:
Changed:
<
<
 gmcs hello.cs  mono hello.exe  
>
>
 gmcs hello.cs
 mono hello.exe
 
  The reason that this is a two-step process hints at the power of C# over interpreted languages; the first step "compiles" your source file into an intermediate byte-code called MSIL which is then processed by a runtime environment, called the Common Language Runtime, or CLR. This type of virtual instruction set is nothing new in computer science. But an innovation that Microsoft's CLR introduced was that this MSIL code is translated, on an as-needed basis, into actual native machine instructions for the target system. And it's retained in this optimal form as the program runs. This is called Just-in-time, or JIT compilation, and it means that your C# program runs with the performance of true native compilation.

In fact, you can even use mono to execute, on patas, an MSIL binary produced by Microsoft Visual Studio 2008! Just copy the .exe file (in binary mode) to patas and run it like so:

Changed:
<
<
 mono compiled_by_msvc.exe  
>
>
 mono compiled_by_msvc.exe
 
  To invoke a mono program from a CONDOR script, you should specify the full path to the mono executable:
universe   = vanilla  

Line: 36 to 41
 error = mystderr.txt log = /tmp/myuwid.log transfer_executable = false
Changed:
<
<
queue
>
>
queue
 
2. Documentation

Microsoft's detailed commercial-quality documentation on C# is available freely on the web. Of primary interest will be the extensive CLR (".NET Framework") class libraries, which provide a wide array of system services and data structures. The mono project also offers a set of documentation.

Line: 48 to 53
 
3. String Manipulation
using System;  
using System.Text;    

Changed:
<
<
static class MainClass
{     

>
>
static class MainClass {
  static void Main(String[] args) { String s = "1.\tThis is a string."; String[] string_arr = s.Split('\t');
Added:
>
>
  Char[] trim_chars = ".:;,".ToCharArray(); String ns = string_arr[0].Trim(trim_chars); int i = ToInt32 (ns);
Added:
>
>
  WriteLine (i);
Added:
>
>
  foreach (String s2 in string_arr[1].Split()) WriteLine (s2.Replace('s','z')); }
Changed:
<
<
}
>
>
}
  Result:
Changed:
<
<
 1  Thiz  iz  a  ztring.  
>
>
 1
 Thiz
 iz
 a
 ztring.
 
  Internally, all strings in C# are Unicode, as is the Char data type. A rich set of functions is provided in the Class Library's 'Encoding' namespace for reading and writing the various 8-bit character sets as input and output.
Line: 75 to 91
  using System.IO; using System.Linq; using System.Text;
Changed:
<
<
static class MainClass {
>
>
static class MainClass {
  static void Main(String[] args) { String my_filename = "the_file.txt";
Added:
>
>
  String data = "Four score and seven years ago.";
Changed:
<
<
// Write some data to the file int i = 0;
>
>
// Write some data to the file int i = 0;
  using (FileStream fs = new FileStream (my_filename, FileMode .Create, FileAccess .Write, FileShare .None))
Changed:
<
<
{ using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1
>
>
{ using (StreamWriter sr = new StreamWriter (fs, GetEncoding (28591))) // Latin-1
  { foreach (String s in data.Split()) sr.WriteLine((++i).ToString() + ". " + new String(s.ToCharArray().Reverse().ToArray())); } }
Added:
>
>
  // Read data from the file using (FileStream fs = File.Open(my_filename, FileMode .Open, FileAccess .Read, FileShare .Read))
Changed:
<
<
{ using (StreamReader sr = new StreamReader (fs, GetEncoding (28591)))
>
>
{ using (StreamReader sr = new StreamReader (fs, GetEncoding (28591)))
  { String s; while (null = (s = sr.ReadLine()))
Line: 99 to 122
  } } }
Changed:
<
<
}
>
>
}
  Result:
Changed:
<
<
 1. ruoF  2. erocs  3. dna  4. neves  5. sraey  6. .oga  
>
>
 1. ruoF
 2. erocs
 3. dna
 4. neves
 5. sraey
 6. .oga
 
 
5. Hash Table of User-defined Objects
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;

Added:
>
>
  public class MyObject { public Double d_x; public Double d_y; public Double d_z;
Added:
>
>
  // constructor public MyObject (Double x_arg, Double y_arg, Double z_arg) {
Changed:
<
<
d_x = x_arg; d_y = y_arg; d_z = z_arg;
>
>
d_x = x_arg; d_y = y_arg; d_z = z_arg;
  } };
Added:
>
>
  static class MainClass
Changed:
<
<
{
     static void Main(String[] args)
     {
        Dictionary<String, MyObject> ht = new Dictionary<String, MyObject>();
        ht.Add("object 1", new MyObject(3.0, 2.1, Math.PI));
        ht.Add("object 2", new MyObject(Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2));
        ht.Add("3rd object", new MyObject(2.1, 9.9, Double.NaN));

>
>
{ static void Main(String[] args) { Dictionary<String, MyObject > ht = new Dictionary<String, MyObject >();

ht.Add("object 1", new MyObject (3.0, 2.1, Math.PI)); ht.Add("object 2", new MyObject (Math.Sqrt(2.0), Math.Log(6.0,10.0), 3.2)); ht.Add("3rd object", new MyObject (2.1, 9.9, NaN ));

  WriteLine (ht["object 2"].d_x); }
Changed:
<
<
}
>
>
}
 
6. Generic Collections

C# has a number of powerful strongly-typed generic collection objects. For example:

Line: 144 to 183
  using System.Collections.Generic; using System.Linq; using System.Text;
Added:
>
>
  static class MainClass { static void Main(String[] args)
Line: 154 to 194
  "Welfare, and secure the Blessings of Liberty to ourselves and our " + "Posterity, do ordain and establish this Constitution for the United " + "States of America.";
Added:
>
>
  Char[] split_chars = new Char[] { '.', ',', ' ' }; String[] words = text.ToLower().Split(split_chars,StringSplitOptions.RemoveEmptyEntries);
Added:
>
>
  // use a hash table to tally word-types: Dictionary<String, int> hash_tab = new Dictionary<String, int>(); foreach (String w in words)
Line: 166 to 208
  else hash_tab.Add(w,1); }
Added:
>
>
  // demonstrate using LINQ to display 12 of the most common word-types plus their counts KeyValuePair <String, int>[] kvp_arr = hash_tab.OrderByDescending(e => e.Value).Take(12).ToArray(); // this is similar to 'list comprehension' in Python String s = kvp_arr.Aggregate(String.Empty,(av, e) => av + e.Key + "[" + e.Value + "] "); WriteLine (s); }
Changed:
<
<
}
>
>
}
  Result:
 the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] 
Line: 182 to 225
  using System.IO; using System.Text; using System.Net;
Added:
>
>
  static class MainClass { static void Main(String[] args)
Line: 197 to 241
  } WriteLine (s); }
Changed:
<
<
}
>
>
}
 
8. LINQ Operations

One of the exciting things about mono is that it includes support for one of the latest developments in Microsoft's C# 3.5, namely Language-Integrated Query (LINQ), and its supporting technologies (extension methods and lambda expressions). LINQ allows sophisticated and concise retrieval and manipulation operations to be executed on data collections via native C# language expressions. Categories of operations include aggregation, quantification, conversion, concatenation, retrieval, set (union, intersection, etc.), generation, grouping, join, ordering, projection, partitioning, and restriction (filtering).

Line: 207 to 252
  using System.Collections.Generic; using System.Linq; using System.Text;
Added:
>
>
  static class MainClass { static void Main(string[] args) { String[] items = { "cat", "pear", "apple", "cat", "banana", "pear", "pear", "apple" };
Added:
>
>
  KeyValuePair [] tallies = items.GroupBy(k => k, e => 1) .Select(f => new KeyValuePair <String, int>(f.Key.ToUpper(), f.Sum())) .OrderBy(g => g.Key) .ToArray();
Added:
>
>
  foreach (KeyValuePair <String, int> kvp in tallies) WriteLine (kvp.Key + '\t' + kvp.Value); }
Changed:
<
<
}
>
>
}
  Result:
Changed:
<
<
 APPLE   2  BANANA  1  CAT     2  PEAR    3  
>
>
 APPLE   2
 BANANA  1
 CAT     2
 PEAR    3
 
 
9. Calling a Python Script as a Shell Process
  using System;
  using System.IO;
  using System.Diagnostics;

Added:
>
>
  class MainClass { public static void Main(string[] args)
Line: 237 to 290
  psi.Arguments = "test.py"; psi.RedirectStandardOutput = true; psi.UseShellExecute = false;
Added:
>
>
  using (Process p = new Process()) { p.StartInfo = psi; p.Start();
Added:
>
>
  WriteLine (p.StandardOutput.ReadToEnd()); } }
Changed:
<
<
}
>
>
}
  -- Main.gslayden - 14 Nov 2008
 
This site is powered by the TWiki collaboration platformCopyright & by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Privacy Statement Terms & Conditions