Nov 22, 2006

Finally managed to learn C#

And it wasn't as hard as I thought it would be. I finally dove into the Ecma C# language specification document, for lack of any better reference/tutorial books here in Dhaka, and went through it absorbing everything that had confused me about the language before (like attributes, the override keyword and inheritance, assemblies and DLLs). A little bit about that spec. I downloaded it a long time ago meaning to go through it and learn the language, but kept putting it off. Now that I've done it, I see the spec is really an excellent reference for learning the language -- probably better than any O'Reilly `In a Nutshell' book for the language. If you're not new to programming, but are new to C#, just get it. It's worth it.

Wish there was an equivalent .NET library reference though. The MSDN library is very thorough but also bloated and slow. Kinda typical of Microsoft when you think about it. Heh, heh.

To celebrate, here's the first useful program I wrote (just now) in C#: ToggleScreenSaver. It's a command-line tool that turns your screen saver on or off. Works only on Windows. Usage is:

ToggleScreenSaver [on|off]

on: turns the screen saver on
off: turns it off

Source code:
// ToggleScreenSaver.cs
using System;
using Microsoft.Win32;

public class ToggleScreenSaver {
static void Main(string[] args) {
// args[0] is the first argument passed and so on, not the name of the program
// That's a little brain-dead, but OK, we can roll with it
RegistryKey rk = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
if (args[0] == "on") {
rk.SetValue("ScreenSaveActive", "1");
} else {
rk.SetValue("ScreenSaveActive", "0");