Topic : Easy C#
Author : Joshua Trupin
Page : 1 Next >>
Go to page :


Summary

SUMMARY: Many developers wish there was a language that was easy to write, read, and maintain like Visual Basic, but that still provided the power and flexibility of C++. For those developers, the new C# language is here. Microsoft has built C# with type-safety, garbage collection, simplified type declarations, versioning and scalability support, and lots of other features that make developing solutions faster and easier, especially for COM+ and Web Services. This article gives you a first look at C#, a language you are going to be hearing lots more about in the very near future.




You may have read recent press accounts of a new programming language that Microsoft has been developing. Well, the language is here. C#, pronounced "C sharp," is a new programming language that makes it easier for C and C++ programmers to generate COM+-ready programs. In addition, C# has been built from the ground up to make it easier to write and maintain programs. It's a little like taking all the good stuff in Visual Basic® and adding it to C++, while trimming off some of the more arcane C and C++ traditions.
Going forward, C# is expected to be the best language from Microsoft® for writing COM+ and Windows®-based programs for enterprise computing. However, you won't have to migrate your existing C or C++ code. If you like the new features in C#—and you probably will—you can migrate your mindset to C#. Let's face it—C++ is a powerful language, but it isn't always a walk in the park. I've used both Visual Basic and C++ professionally, and after a while I was asking myself why I needed to implement every last destructor for every last C++ class. C'mon already—you're a smart language. Visual C++® even has IntelliSense®. Clean up after me. If you like C and C++, but sometimes think like I do, C# is for you.

The main design goal of C# was simplicity rather than pure power. You do give up a little processing power, but you get cool stuff like type safety and automatic garbage collection in return. C# can make your code more stable and productive overall, meaning that you can more than make up that lost power in the long run. C# offers several key benefits for programmers:


Simplicity
Consistency
Modernity
Object-orientation
Type-safety
Scalability
Version support
Compatibility
Flexibility
Let's look at each of the ways that C# stands to improve your coding life.  Simplicity
What's one of the most annoying things about working in C++? It's gotta be remembering when to use the -> pointer indicator, when to use the :: for a class member, and when to use the dot. And the compiler knows when you get it wrong, doesn't it? It even tells you that you got it wrong! If there's a reason for that beyond out-and out taunting, I fail to see it.
C# recognizes this irksome little fixture of the C++ programming life and simplifies it. In C#, everything is represented by a dot. Whether you're looking at members, classes, name-spaces, references, or what have you, you don't need to track which operator to use. Okay, so what's the second most annoying thing about working in C and C++? It's figuring out exactly what type of data type to use. In C#, a Unicode character is no longer a wchar_t, it's a char. A 64-bit integer is a long, not an __int64. And a char is a char is a char. There's no more char, unsigned char, signed char, and wchar_t to track. I'll talk more about data types later in this article.

The third most annoying problem that you run across in C and C++ is integers being used as Booleans, causing assignment errors when you confuse = and ==. C# separates these two types, providing a separate bool type that solves this problem. A bool can be true or false, and can't be converted into other types. Similarly, an integer or object reference can't be tested to be true or false—it must be compared to zero (or to null in the case of the reference). If you wrote code like this in C++:


int i;
if (i) . . .


You need to convert that into something like this for C#:


int i;
if (i != 0) . . .


Another programmer-friendly feature is the improvement over C++ in the way switch statements work. In C++, you could write a switch statement that fell through from case to case. For example, this code

switch (i)
{
    case 1:
        FunctionA();

    case 2:
        FunctionB();
        Break;
}


would call both FunctionA and FunctionB if i was equal to 1. C# works like Visual Basic, putting an implied break before each case statement. If you really do want the case statement to fall through, you can rewrite the switch block like this in C#:


switch (i)
{
    case 1:
        FunctionA();
        goto case 2;

    case 2:
        FunctionB();
        Break;
}


Consistency


C# unifies the type system by letting you view every type in the language as an object. Whether you're using a class, a struct, an array, or a primitive, you'll be able to treat it as an object. Objects are combined into namespaces, which allow you to access everything programmatically. This means that instead of putting includes in your file like this


#include
#include
#include


you include a particular namespace in your program to gain access to the classes and objects contained within it:


using System;
In COM+, all classes exist within a single hierarchical namespace. In C#, the using statement lets you avoid having to specify the fully qualified name when you use a class. For example, the System namespace contains sev-eral classes, including Console. Console has a WriteLine method that, as you might expect, writes a line to the system console. If you want to write the output part of a Hello World program in C#, you can say:


System.Console.WriteLine("Hello World!");

This same code can be written as:


Using System;
Console.WriteLine("Hello World!");


That's almost everything you need for the C# Hello World program. A complete C# program needs a class definition and a Main function. A complete, console-based Hello World program in C# looks like this:


using System;

class HelloWorld
{
    public static int Main(String[] args)
    {
        Console.WriteLine("Hello, World!");
        return 0;
    }
}


The first line makes System—the COM+ base class namespace—available to the program. The program class itself is named HelloWorld (code is arranged into classes, not by files). The Main method (which takes arguments) is defined within HelloWorld. The COM+ Console class writes the friendly message, and the program is finished. Of course, you could get fancy. What if you want to reuse the HelloWorld program? Easy—put it into its own namespace! Just wrap it in a namespace and declare the classes as public if you want them accessible outside the particular namespace. (Note here that I've changed the name Main to the more suitable name SayHi.)


using System;

namespace MSDNMag
{
    public class HelloWorld
    {
        public static int SayHi()
        {
            Console.WriteLine("Hello, World!");
            return 0;
        }
    }
}


You can then compile this into a DLL, and include the DLL with any other programs you're building. The calling program could look like this:


using System;
using MSDNMag;

class CallingMSDNMag
{
    public static void Main(string[] args)
    {
        HelloWorld.SayHi();
        return 0;
    }
}


One final point about classes. If you have classes with the same name in more than one namespace, C# lets you define aliases for any of them so you don't have to fully qualify them. Here's an example. Suppose you have created a class NS1.NS2. ClassA that looks like this:


namespace NS1.NS2
{
    class ClassA {}
}


You can then create a second namespace, NS3, that derives the class N3.ClassB from NS1.NS2.ClassA like this:

namespace NS3
{
    class ClassB: NS1.NS2.ClassA {}
}


If this construct is too long for you, or if you're going to repeat it several times, you can use the alias A for the class NS1.NS2.ClassA with the using statement like so:


namespace NS3
{
    using A


Page : 1 Next >>