Difference: UsingCSharpOnPatas (12 vs. 13)

Revision 132009-10-28 - gslayden

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

Using C# on Patas

Added:
>
>
October 2009 : Mono upgraded to 2.4.2.3 on Patas. Thanks David!
 C# is a powerful general-purpose programming language originally developed by Microsoft. After its approval as a standard by ECMA, it has been independently re-engineered as an open-source implementation which is available on many platforms, including Linux. It is installed and available for use on patas.

Because the mono implementation is extremely compatible, Console-oriented C# programs developed on Windows machines (i.e. with Visual Studio) will typically run on mono without changing the source code. (It is beyond the scope of this document to discuss the compatibility of graphical programs.)

Changed:
<
<
C# programs operate in a fully sandboxed runtime environment which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer.
>
>
C# programs operate in a fully sandboxed runtime environment (the Common Language Infrastructure, or CLI) which provides garbage-collection. As in Java, Python, and other high-level general-purpose languages, disposal of unused memory objects is tracked and managed by the system, relieving an enormous burden from the application programmer.
 
1. Sample Program
 using System;
 using System.Text;

Line: 81 to 83
  ztring.
Changed:
<
<
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.
>
>
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.
 
Changed:
<
<
As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending.
>
>
As in many other modern languages, the C# String type is immutable. This allows for significant internal optimizations in the runtime environment but can be inefficient when doing intensive mutation and other editing operations. For this reason, the CLR also implements the StringBuilder object, which allows a large amount of text to be gathered through appending.
 
Changed:
<
<
Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char.
>
>
Strings can also be edited by converting them to an array of Char, as shown in the next example, "Reading and Writing Files." The example also shows how a String object can be created from an array of Char.
 
4. Reading and Writing Files
 using System;
 using System.IO;

Line: 219 to 221
 

Result:

Changed:
<
<
 the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1] 
>
>
the[6] of[3] and[3] establish[2] for[2] states[2] to[2] united[2] perfect[1] promote[1] general[1] we[1]
 
7. Access a Web Page
 using System;
 using System.IO;

Line: 301 to 304
  } }
Added:
>
>
10. Using the GroupBy LINQ operator

We have found that, even with the new release 2.4.2 of Mono, the GroupBy operator is much slower than it is on .NET. It is to be expected that you will find some differences in performance between the Microsoft and Mono implementations. In the case of GroupBy, you can always break the operation in two steps, using a temporary dictionary, for example.

Keeping this limitation in mind, here is the example I presented at the 2009 CLMA orientation talk on C#. This complete program reads an HTML file with the complete text of "Moby Dick," strips out HTML tags using a regular expression, and then prints a "graphical" Zipf distribution (word-frequency histogram) of the 35 most common words onto your console.

using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

class MainClass
{
	static void Main()
	{
		String text = new StreamReader("moby_dick.html").ReadToEnd();

		text = Regex.Replace(text, "<(.|\n)*?>", String.Empty);

		String[] words = text.Split(" \n\",.;-!?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

 
Changed:
<
<
-- Main.gslayden - 14 Nov 2008
>
>
var grp = words.GroupBy(w => w.ToLower()).ToArray();

var tallies = words .GroupBy(w => w.ToLower()) .Select(g => new { g.Key, Tally = g.Count() }) .OrderByDescending(e => e.Tally);

int scale = tallies.First().Tally / 60; foreach (var tally in tallies.Take(35)) WriteLine ("{0,6} {1}", tally.Key, new String('*', tally.Tally / scale)); } };

11. More Information

A recording of the treehouse presentation "Using C# on Patas" can be found here: http://uweoconnect.extn.washington.edu/p30062745/. The slides from this talk are on patas at /opt/dropbox/09-10/orientation/Mono_on_Patas.pdf.

Most of the code examples on this page, along with the "Moby Dick" text, can be found on patas in the following directory: /opt/dropbox/09-10/orientation/csharp-demos

The best book on LINQ that I've found is "Pro LINQ: Language Integrated Query in C# 2008" by Joseph C. Rattz, Jr. (APress 1-59059-789-3). This book presupposes familiarity with C#.

-- Main.gslayden - 28 Oct 2009

12. Discussion
  If anybody has any info about how to get a good installation of mono onto ubuntu 8.0.x, I would really appreciate hearing about it. I put about 8 hours into trying to to install mono on Ubuntu, and that is about 6 more hours than I really had to spend.
Line: 317 to 361
 You could also experiment with some of the RPMs here, and see if any of them will install using "alien": http://download.opensuse.org/repositories/Mono/ Note that many of these are for mono 1.9.x. To get 2.x on patas I had to build it from source.

-- brodbd - 30 Dec 2008

Added:
>
>
-----Original Message-----
From: patas-announce-bounces@mailman2.u.washington.edu On Behalf Of brodb@u
Sent: Thursday, October 15, 2009 3:34 PM
To: patas-announce@u.washington.edu
Subject: [patas-announce] Mono upgraded to 2.4.2.3.

I've updated our local build of Mono, in /opt/mono/bin, to 2.4.2.3.  Mono Debugger (mdb) version 2.4.2.1 is also installed there.

The old version remains available in /opt/mono-2.0.1/bin, if you need it.
 
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