log in

Learning C# coming from Java

2008-02-18 19:40 UTC

Learning C#

From a Java background

Starting off I am wondering where exactly that starting line is, because learning another language is difficult to find the exact place where you can begin without losing any information. I have a decent background in Java, slightly less so in C, and am pretty familiar with JavaScript and Perl, so I've been around the block a few times. However, with previous languages there have been a shift of paradigm, or a complete difference in syntax from what I'm used to. From what C# I've seen, it's basically Java implemented differently.

That lead me to believe I should just jump in. So, here I am. My first question was "What the heck are namespaces?", because they're not quite in Java. Asking Luke gave me the following response:
(3:02:46 PM) Victor: one thing you need to know is that, at the metadata level, there is no connection between NamespaceA and NamespaceA.NamespacePseudoChild

Welp! That's a mouthful. And a handful. All I got out of that was there is no inheritance by name. Initially, they sound like packages in Java, but not sure exactly. So, I'll go about thinking of them as such, and revel in the freedom C# gives one as to declaring multiple public classes in one file, and more than one namespace (even though that's probably a bad way to do things). Oh well, I'll ignore it for now and come back to it later if/when it's more important.

So to start in, I'm going to implement an LS clone in C#. I've done this project before in C, so it should be relatively simple. In C, the hardest part was to figure out what to pass quicksort, and how to arrange the items present.

Next hurdle: reading through a sample implementation of Code Fragment of Mono.getOptions and Using Mono.getOptions to process command line input, I stumbled upon "Class Opts : Options". Gosh, that is some pretty inheritance syntax.

VS2k5 Express' automatic indentation is really cool.

My comrade-in-arms Bobua asked last night why his console app kept disappearing, so I told him to throw in some line asking for user input because the program will stop until you hit a key. However! finding out the syntax for user input was somewhat difficult, and it took a few Google searches. Eventually I found this link C# user input by searching "C# user input" on Google. MSDN didn't work; it gave me some crazy keyboard mapping stuff. "C# keyboard" didn't work; it gave me automated keystrokes. It was really annoying to find that one link, though it ended up being extremely helpful. It has the answer (Console.ReadLine()), as well as links to other tutorials that looked good. Still - that search took far too long. Perhaps I don't know what to look for yet.

I keep having these "COOL" moments with C#. It has a lot of features that are really awesome, such as the following:
public static void Main(){
   Array dayArray = Enum.GetValues(typeof(EnumTest.DAYS));
   foreach (DAYS day in dayArray) 
   Console.WriteLine("Number {1} of EnumTest.DAYS is {0}", day,day.ToString("d")); 

Which is taken from the Sharpsteel C# Tutorial, found in the link above. Do you see it?? The really cool part(s)?? Okay fine. I'll tell you - the C style arguments for printing. Maybe I stretched "C style" a little bit much, but the {0} and {1} are reminiscent of C's %i and %s, even though they aren't declaring types. They remind me because of the way they're used as placeholders in the format string, which is pretty cool. Also, day.ToString("d") is cool since you can change the format of your argument to decimal.

I've finally found one thing about Java that is better than C#. The "imports" of C#, "Using", are crazy. Imports are relatively straight-forward, much like the #include's of C before it. While I'm not entirely sure how they work with the compiler, they do let you know if you've imported something incorrectly before compile time. C#'s syntax does not! Seeing no errors with System.Linq or Mono.getOptions, I tried to compile, only getting some awesome errors alluding to the fact that C# can't find what I'm trying to include. What!? There were no underlines! Call me new-fashioned for liking the underlines for correct imports, but it is useful. So, to get those working, I find that I need to include references to the project (not done automagically), and download Mono (not surprising, though I did think it was there for a short time). Thankfully there is a rather useful "include references" menu, with several tabs and a list of all the .Net includes, and a tab for importing .dll's from other locations. This import tab is necessary for including Mono.GetOptions. The case IS significant, yes, much like in Perl.

Documentation for Mono.GetOptions is pretty awesome though, with 1/8 classes/delegates/enumerations documented.

Update: I have finally finished implementing LS; it only took this long due to lack of time spent on it. While it is not a full clone, it does introduce one to several nuances of the language as it is pretty simple. I only implemented options such as 'sort in reverse order' and 'sort by last access time' and 'sort by size.' These are REALLY easy with the Collections.Generic.List class, for which you can implement your own comparator pretty easily. Syntactically I have few complaints, and it was a good learning experience.

The next project I have started on is a clone of Notepad, or something similar; perhaps more extensible. The aim of this is to learn more C#, and ultimately to reproduce the functionality of Luke's RegexTest program, which was written in VB.Net. Along the way, I plan on modularizing the code so that if there is another project in the future that needs a text editor, this one can be easily modified to suit the purpose.

Windows forms documentation is pretty bad, but once you figure out how to drag and drop from the toolbar, instantiate objects from there that you will need, and observe some functional code to get an idea of how it all ties together, it's reasonably easy. There is no drawing involved, and so far, no tricky manipulations to get the look I want. Provided, of course, that it is a Windows look I'm going for.

Stumbling points: "How the HELL do I do that!?" ... 10 minutes later ... "oh. It's built in. argh."

"Where do I find x?" <-- MSDN is pretty good for these though.

How to actually modularize the code in a usable and extensible manner.