<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>centriment &#187; c</title>
	<atom:link href="http://www.centriment.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.centriment.com</link>
	<description>stories of the spirit living inside the computer</description>
	<lastBuildDate>Sun, 26 Jul 2009 12:29:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Interactive C# (REPL)</title>
		<link>http://www.centriment.com/2009/06/18/using-interactive-c-repl/</link>
		<comments>http://www.centriment.com/2009/06/18/using-interactive-c-repl/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 08:42:50 +0000</pubDate>
		<dc:creator>Jan Dzik</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mono/.NET]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[gsharp]]></category>
		<category><![CDATA[gtk#]]></category>
		<category><![CDATA[interactive]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[repl]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.centriment.com/?p=226</guid>
		<description><![CDATA[The REPL, that is Read Eval Print Loop, is a feature provided by many programming language environments today, such as Python, Ruby, Scala and Haskel. For those who are not familiar with the notion, you could think of REPL as an interactive interpreter (even though it&#8217;s not always implemented as one). You can find more [...]]]></description>
			<content:encoded><![CDATA[<p>The REPL, that is Read Eval Print Loop, is a feature provided by many programming language environments today, such as Python, Ruby, Scala and Haskel. For those who are not familiar with the notion, you could think of REPL as an interactive interpreter (even though it&#8217;s not always implemented as one). You can find more info <a title="REPL by WikiPedia" href="http://en.wikipedia.org/wiki/REPL" target="_blank">here</a>.</p>
<p>It practically is an interactive prompt (like a UNIX shell, or Windows PowerShell), that accepts expressions in your favorite programming language syntax. There was no practical implementation of a REPL for C# until the Mono project <a title="Mono Project C# REPL implementation" href="http://www.mono-project.com/CsharpRepl" target="_blank">implemented</a> a C# Interactive Shell in Mono 2.2, called <em>csharp</em>. It comes with two flavors: 1). as a command line shell under the name <em>csharp</em> and 2). with a graphical front end called <em>GSharp.</em></p>
<p>I am sure that every developer, novice or seasoned, needs to test out some small snippet of code, from time to time, to make sure that it does what it is supposed to do. This is also true for C# developers. However, in the absence of a REPL for C#, the only way to do that was by creating a whole new project in Visual Studio or MonoDevelop with a very simple console application. Not only it seems like an overkill if you just want to test a few lines of code, but you also end up with a bunch of little test projects that you are never going to need.</p>
<p>However, now that Mono implemented a C# REPL there is no need to create a test console project for everything. It is extremely useful if you want to test out some piece of code.</p>
<p>An example from my own experience is when, in a C# project I have been working on, I needed to dynamically create a list of enum field values and their string names. I was not 100% percent sure of the way I thought of implementing this, so I wanted to test it out first.</p>
<p>Here it is, in the <em>csharp</em> interactive C# shell, running from a Linux command line:</p>
<pre>anirothan@netbook:~$ mono-2.4 csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp&gt; var fileModeNames = Enum.GetNames(typeof(System.IO.FileMode));
csharp&gt; fileModeNames;
{ "CreateNew", "Create", "Open", "OpenOrCreate", "Truncate", "Append" }
csharp&gt; var fileModeValues = Enum.GetValues(typeof(System.IO.FileMode));
csharp&gt; fileModeValues;
{ CreateNew, Create, Open, OpenOrCreate, Truncate, Append }
csharp&gt; var pairs = new Dictionary&lt;string, System.IO.FileMode&gt;();
csharp&gt; for(int i=0; i&lt;fileModeValues.Length; ++i)
 &gt; pairs.Add(fileModeNames[i], fileModeValues.GetValue(i));
{interactive}(2,7): error CS1502: The best overloaded method match for `System.Collections.Generic.Dictionary&lt;string,System.IO.FileMode&gt;.Add(string, System.IO.FileMode)' has some invalid arguments
/opt/mono-2.4/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
{interactive}(2,7): error CS1503: Argument `#2' cannot convert `object' expression to type `System.IO.FileMode'
csharp&gt; var pairsCorrect = new Dictionary&lt;string, object&gt;();     
csharp&gt; for(int i=0; i&lt;fileModeValues.Length; ++i)               
 &gt; pairsCorrect.Add(fileModeNames[i], fileModeValues.GetValue(i));
csharp&gt; pairsCorrect;
{{ "CreateNew", CreateNew }, { "Create", Create }, { "Open", Open }, { "OpenOrCreate", OpenOrCreate }, { "Truncate", Truncate }, { "Append", Append }}
csharp&gt; pairsCorrect["CreateNew"]
 &gt; ;
CreateNew
csharp&gt; quit;
null</pre>
<p>That was fairly easy and simple. The intention was to create a dictionary of enum field names and values. For the sake of testing I used the<em> System.IO.FileMode</em> enum.</p>
<p>The flexibility of the csharp REPL does not end there. Here is an extract from a session in gsharp (The gtk+ graphical front-end to csharp) which actually creates a window with a dropdown list.</p>
<pre>csharp&gt; LoadPackage("gtk-sharp-2.0");
csharp&gt; using Gtk;
csharp&gt; var window = new Window("Simple dropdown list binding test with csharp REPL");
csharp&gt; var vbox = new VBox();
csharp&gt; var label = new Label("This is a simple test.");
csharp&gt; vbox.PackStart(label);
csharp&gt; var model = new ListStore(typeof(System.IO.FileMode), typeof(string));
csharp&gt; var fileModeNames = Enum.GetNames(typeof(System.IO.FileMode));
csharp&gt; fileModeNames;
{ "CreateNew", "Create", "Open", "OpenOrCreate", "Truncate", "Append" }
csharp&gt; var fileModeValues = Enum.GetValues(typeof(System.IO.FileMode));
csharp&gt; fileModeValues;
{ CreateNew, Create, Open, OpenOrCreate, Truncate, Append }
csharp&gt; var model = new ListStore(typeof(System.IO.FileMode), typeof(string));
csharp&gt; for(int i=0; i&lt;fileModeValues.Length; ++i)
 &gt; model.AppendValues(fileModeValues.GetValue(i), fileModeValues[i]);
{interactive}(2,48): error CS0021: Cannot apply indexing with [] to an expression of type `System.Array'
csharp&gt; for(int i=0; i&lt;fileModeValues.Length; ++i)
model.AppendValues(fileModeValues.GetValue(i), fileModeNames[i]);
csharp&gt; var dropDown = new ComboBox();
csharp&gt; var renderer = new CellRendererText();
csharp&gt; dropDown.PackStart(renderer, true);
csharp&gt; dropDown.AddAttribute(renderer, "text", 1);
csharp&gt; dropDown.Model = model;
{ { CreateNew, "CreateNew" }, { Create, "Create" }, { Open, "Open" }, { OpenOrCreate, "OpenOrCreate" }, { Truncate, "Truncate" }, { Append, "Append" } }
csharp&gt; vbox.PackEnd(dropDown);
csharp&gt; window.Add(vbox);
csharp&gt; window.ShowAll();
csharp&gt;</pre>
<div id="attachment_249" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.centriment.com/wp-content/uploads/2009/06/REPL-GTK.png"><img class="size-medium wp-image-249" title="REPL-GTK#" src="http://www.centriment.com/wp-content/uploads/2009/06/REPL-GTK-300x175.png" alt="The GSharp C# Interactive Shell in action." width="300" height="175" /></a><p class="wp-caption-text">The GSharp C# Interactive Shell in action.</p></div>
<p>There are some limitations though. For example, you cannot create classes (but you can put .cs files in the <em>~/.config/csharp</em> directory that will be loaded automatically to the interactive shell) and there is an apparent lack of code completion features. However, these limitations are already being worked on and I hope they will become available with mono 2.6.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.centriment.com/2009/06/18/using-interactive-c-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The one about &#8220;Hello World&#8221;</title>
		<link>http://www.centriment.com/2009/03/12/the-one-about-hello-world/</link>
		<comments>http://www.centriment.com/2009/03/12/the-one-about-hello-world/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 21:19:38 +0000</pubDate>
		<dc:creator>Jan Dzik</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[misc]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.centriment.com/?p=3</guid>
		<description><![CDATA[Hello World!!
This is my first blog post! I am very excited about starting my own blog. I have been a software engineer for many years now and I have always thought about sharing my experiences in the field of computing with others, through a place of my own, but I never seemed to have the [...]]]></description>
			<content:encoded><![CDATA[<p>Hello World!!</p>
<p>This is my first blog post! I am very excited about starting my own blog. I have been a software engineer for many years now and I have always thought about sharing my experiences in the field of computing with others, through a place of my own, but I never seemed to have the time to get down with it. Finally, I&#8217;ve setup my own domain, installed wordpress, installed a theme and while writing this I am still experimenting a bit with it.</p>
<p>Since the first thing we do when learning a new programming language is to write a &#8220;hello world&#8221; program, and this is the first post in this blog, this is about&#8230; &#8220;Hello Word&#8221;!</p>
<p>And here it is in <em>C</em>, the first &#8220;real&#8221; programming language I&#8217;ve learned:</p>
<pre name="code" class="c">#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

int main(int argc, char *argv[])
{
  printf("Hello Word!");
  return EXIT_SUCCESS;
}</pre>
<p>Here it is in C#:</p>
<pre name="code" class="c#">using System;
public class Application
{
  public static void Main(string[] args)
  {
    Console.WriteLine("Hello World!");
  }
}</pre>
<p>And here it is in Python:</p>
<pre name="code" class="python">print "Hello World!"</pre>
<p>Every time you start to learn a new language or system you begin by making a simple &#8220;hello world&#8221; application. It is not useful, it does not do anything, it&#8217;s plain stupid code. However, it&#8217;s all the most exciting because that&#8217;s when you start learning something new and unknown to you. Even such simple code has a lot of knowledge to convey to its author. First of all, you learn if the code has to be compiled and interpreted, if it is compiled to machine code or some other intermediate language executed by a virtual machine. You are also exposed to the basic rules and coding styles. You learn if a language is object oriented, loosely or strictly typed.</p>
<p>Soon after that, you start writing a little less trivial things and start feeling like a kid with a new toy. As you learn even more, you start feeling like a little god that can meddle with his own privately created universe.</p>
<p>And now this is my &#8220;hello world&#8221; in the blog world!</p>
<p>Remember your &#8220;hello world!&#8221;s?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.centriment.com/2009/03/12/the-one-about-hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
