My problems with C# and .NET

by Miguel de Icaza

I love C# very much, and the new extensions that are being added to C# will improve the language in many areas:

  • Iterators: they simplify authoring the IEnumerable/IEnumerator interfaces in a class. Very nice.
  • Anonymous methods: reduce the glue required for small use cases. For large cases, you dont want to use them anyways, as they make your code look ugly (remember: if a function does not fit on the screen, its probably buggy).

Those will reduce the amount of coding required in some scenarios, but they do not address some of the most common problems that programmers face: strings and regular expressions.

.NET ships with a very powerful regex engine. The engine is so powerful, that it is possible to use it to implement very advanced regexp-based applications. Its as easy to use as the POSIX regex API is: slightly more verbose due to its OO heritage, but in general, very similar.

The problem is that there is no simple way of using this API from a language like C#: the developer is stuck with this low-level API. Perl and Python both bundle powerful regular expression engines that are bound in a fairly usable way in the language.

Its a shame that C# does not have any simple way, or a nice syntactic sugar to manipulate this common idiom.

Strings manipulation in C# falls on the same category: some work was done, but it was not really completed. Adding a few overloads to the class libraries would make C# a sweet language for string processing.

A two parameter indexer: string this [int start, int end] would clear a lot of the confussion with Substring arguments (a previous log entry has more details).

Although we could add this to Mono, the result would not be portable to .NET.

Finally, there is the Stream problem. Reading a file in .NET involves two steps: opening the stream, and then creating a streamreader to read from it. The code looks a bit like this:

	Stream st = File.OpenRead ("notes.txt");
	StreamReader re = new StreamReader (st);
        string line;
			
	while ((line = re.ReadLine ()) != null)
		Console.WriteLine (line);

Wouldn't it be nicer, if you could just do:

	string line;
	while ((line = File.OpenRead ("notes.txt")) != null){
		Console.WriteLine (line);
	}

The above could be easily implemented by having the FileStream implement IEnumerator as Guido does in Python.

Posted on 15 Aug 2003