Computer Science Canada [WIP] C# Whirlwind |
Author: | rdrake [ Fri May 11, 2007 6:58 pm ] | ||||||||||||||||||||||||||||||||||||||||||||||||||
Post subject: | [WIP] C# Whirlwind | ||||||||||||||||||||||||||||||||||||||||||||||||||
C# Whirlwind What is C# and The .NET Framework? C#, pronouced "C Sharp," is a language designed by Microsoft and certified by the EMCA. It is a major language in the .NET Framework and one of the most widely used. It has many simiarities to the Java language syntax-wise. The .NET Framework, pronounced "Dot Net," is a large collection of class libraries written by Microsoft to rapidly create applications. The Framework includes many powerful libraries, making it much easier to develop on. The .NET vision was first outlined by Bill Gates in 2001, with release 1.0 occurring not too much later. The upcoming version of .NET is 3.0 and it promises even more enhancements and large changes. C# combined with the .NET Framework is a powerful combination. Applications can be built quickly and easily, with a large developer community backing both technologies.Similarly to Java, C# requires a runtime environment be installed. When users wish to run your application they will need the .NET runtime package to be installed. How Can I Get .NET and C#? If you are running Microsoft Windows, chances are you have the .NET Framework installed already. If not, version 2.0 is the current version and it is available from the Microsoft download site here. The SDK includes many useful tools for debugging your applications, it also includes a compiler which we need to compile our applications. I still recommend version 2.0 as it is known to be stable, and since 3.0 has not been fully deployed yet. Not on Windows? Novell sponsors a wonderful cross platform implementation of the .NET framework complete with a C# compiler and all. Its name is Mono, and it can be found here. Please note while nearly complete, it still is not 100% complete. Some major components (Windows Forms being a big one) are still not fully implemented. For the time being we need not worry about the omissions. Basic Data Types C# has all your basic data types like integers, booleans, strings, and many more. You can even declare your own types with custom classes. Variable declaration is also very similar to many other C-like languages.
Hello, World! An example C# program would be the following hello world example:
Just for comparison the following is an equivalent Java version:
You compile the C# example with the following line:
Alternatively using Mono:
How Does The Above Work?
More Methods Ok so enough of our single example, let's write more complicated programs. Classes you write can have as many methods in them as you'd like. Typically your Main() method won't do everything the class needs to do. In fact, you only ever need one Main() method in your entire application. Let's revisit our Hello World example:
A Little I/O You've already seen output, but what about input? Input is achieved via many ways, the easiest of which is the "ReadLine()" method. It reads in a line and assigns the value to a string.
Ok, Enough With Hello World Let's create a class representing a real world object, a Dog.
Inheritance, A Programmer's Best Friend Sometimes you will come across situations where a collection of objects share similar properties, but vary in some way. Going back to our dog example, some dogs yip while other dogs bark... and some dogs even howl.
In the Beagle class we override this method by using the "override" keyword. Please note, the method signature (ie. output type, name, and accessibility modifier) must match that of the overridden class. This behaviour is exploited quite often by developers writing their own classes. There are many cases where one would want to override the "ToString()" method in their own classes. Properties There are many instances where we might wish to store certain values in an object. Such values are typically stored as properties of the object. For example, we may wish to store things like the fur colour and name of the dog in the class.
Partying With Polymorphism You may run into situations where you need a method with a certain name to do many things. Let's say you want to modify the Dog and Beagle classes so instead of specifying the name and fur colour after the fact, you can specify them when creating the instance of the class. The problem is you want to keep it the way it was before too, so the user can enter in the name of the dog and the fur colour later on if they wish. This is simple enough to accomplish via polymorphism.
A Little With Control Flow Let's take a break from talking about methods and classes and talk about control flow. Control flow in C# is generally the same as in C-like languages. I'll just demonstrate an example of each of the major ones. If Statement
Switch There are cases where if statements would be too repetative. In this case, we use a switch.
While and Do While Loops An example of a while loop:
An example of a do while loop:
You would use a do while loop instead of a normal while loop in cases where you wish for the code block to execute at least once before termination. For and Foreach For loop:
Foreach loop:
Generic Collections, Another One of Man's Best Friends I'm sure many of us have heard of arrays, problem is they have a fixed length and take time to copy over in order to expand or shrink. The solution? Collections. Collections can store virtually any type of data in them and are great for passing around groups of objects. Let's create a demonstration of a collection:
We solve this by using generic classes, which are type safe:
A Note About Style If you were paying attention to the above code snippets, you would have noticed a certain style. Things like language keywords are always written in lower case. Writing in upper case will lead to syntax errors. Variables always follow camlCase, with the exception of properties which follow PascalCase. Method names and class names are always written in PascalCase as well. Properties of a class are _WrittenLikeThis, with an underscore before its name. Also note the braces begin on the next line. The only case where I do not follow this is when declaring setters/getters. Comments should be full sentences whenever possible written on the line above the line or block it is a comment for. Comments are written inline with the code they refer to and never appear beside a line of code. Further Reading and Conclusion There are a number of great resources out there:
Well, I hope this was somewhat informative and enjoyable. It's my first Whirlwind and I haven't proofread it yet, so if you find any mistakes, please contact me and I'll correct them. If there's a concept I did not explain enough of, please let me know as well. C# is a large language and it is difficult to cover everything, if you wish to have something covered, contact me ![]() |
Author: | haskell [ Fri May 11, 2007 10:25 pm ] |
Post subject: | RE:[WIP] C# Wirlwind |
There are many open-source implementations of the .NET framework besides Mono. DotGNU is one of them. SharpDevelop is a very good open-source IDE for C#/VB.NET. It has many of the same features as Visual Studio, with many possible add-ons so you can get your desired environment. |
Author: | rdrake [ Sat May 12, 2007 1:37 am ] | ||||
Post subject: | Re: [WIP] C# Wirlwind | ||||
A quick correction to one of the sections above. When using collections and generic collections, we must include the proper using statement. Unfortunately I cannot edit my own post, so I'll show how it should have looked here. Generic Collections, Another One of Man's Best Friends I'm sure many of us have heard of arrays, problem is they have a fixed length and take time to copy over in order to expand or shrink. The solution? Collections. Collections can store virtually any type of data in them and are great for passing around groups of objects. Let's create a demonstration of a collection:
We solve this by using generic classes, which are type safe:
Figures the only code I don't test is the code that is not correct... |
Author: | wtd [ Sat May 12, 2007 1:48 am ] |
Post subject: | RE:[WIP] C# Wirlwind |
Good start. |
Author: | wtd [ Sun May 13, 2007 4:38 pm ] |
Post subject: | RE:[WIP] C# Whirlwind |
Delegates! |
Author: | rdrake [ Sun May 13, 2007 9:51 pm ] | ||||||
Post subject: | Re: [WIP] C# Whirlwind | ||||||
Looking to the Future: C# 3.0 Slated for inclusion in version 3.5 of the .NET Framework, C# 3.0 promises a number of helpful features many of us find in various languages, but miss in C# 2.0. There are a number of changes, but I will only cover a few for now. Implicitly Typed Local Variables Yes, this we have taken advantage in languages like Ruby for a long time now, and they've finally made it to C#. Let's take a look at some old 2.0 code, then its 3.0 replacement.
Object Initializers I'm sure anybody here whose written C# or Java classes realizes that properties and class constructors are all basically redundant code. Well, you have more options now thanks to object initializers and a new short hand for getters and setters (ie. public methods that control access to properties in a class). Consider an updated version of my favourite above code example:
|
Author: | rdrake [ Sun Jul 22, 2007 12:23 am ] | ||||||||||||||||||||||
Post subject: | Re: [WIP] C# Whirlwind | ||||||||||||||||||||||
Since learning about conventions in a particular language helps you understand previously written code, and can also help you write more understandable code for when you need help, I've decided to go over some of the basics one should know. Let's start off with an example:
Ok, let's go back to one of the examples I wrote earlier, only in a smaller form:
Some more:
Something else you may have noticed, "PI" is written in all caps. Generally anything from 1-2 letters may remain all caps, anything longer is written in (you guessed it) Pascal case. You can notice this in the .NET BCL naming conventions.
Typically you use block comments (/* */) to document your source files in some languages. You should always use the following way for C#:
Multiple member variables are usually declared on their own line:
Well, I'm sure a missed a few conventions but those are some of the main ones that are generally used. Do you have to agree with these conventions? No. However, this is generally what I see and write when it comes to C#. My apologies to those who had to suffer through my disorganization of this. |