> mono-winforms

> mono-extras

That gives you the basics to do Mono development, plus a few extra bits and pieces if you want to branch out a bit.

If you want to do some exciting things with Mono, lots of Mono-enabled libraries are available. Try going to the Search view and search for 'sharp' to bring up the list of .NET-enabled libraries that you can use with Mono — the suffix is used because C# is the most popular .NET language. In this list, you'll see things such as avahi-sharp, dbus-sharp, evolution-sharp, gecko-sharp2, gmime-sharp, gnome-sharp, gtk-sharp2, gtksourceview-sharp, and ipod-sharp — we recommend you at least install the gtk-sharp2 libraries (including the development package) as these are used to create graphical user interfaces for Mono.

But for now, let's get you up and running with Mono on the command line. Mono is split into two distinct parts: the compiler and the interpreter. The compiler turns your source code into an executable, and is called gmcs. The interpreter actually runs your code as a working program, and is just called Mono. You should by now have installed MonoDevelop, so go the Applications menu, choose Programming, then MonoDevelop to start it up.

TIP

You don't have to use MonoDevelop to write your code, but it helps — syntax highlighting, code completion, and drag-and-drop GUI designers are just a few of its features.

When MonoDevelop has loaded, go to the File menu and choose New Project. From the left of the window that appears, choose C#, then Console Project. Give it a name and choose a location to save it — all being well, you should see something similar to Figure 29.1. When you're ready, click New to have MonoDevelop generate your project for you.

FIGURE 29.1 MonoDevelop ships with a number of templates to get you started, including one for a quick Console Project.

The default Console Project template creates a program that prints a simple message to the command line: the oh-so-traditional 'Hello World!' Change it to something more insightful if you want, then press F5 to build and run the project. Just following the code view is a set of tabs where debug output is printed. One of those tabs, Application Output, becomes selected when the program runs, and you'll see 'Hello World!' (or the message you chose) printed there — not bad given that you haven't written any code yet! You can see how this should look in Figure 29.2.

FIGURE 29.2 Your console template prints a message to the Application Output window in the bottom part of the window.

The Structure of a C# Program

As you can guess from its name, C# draws very heavily on C and C++ for its syntax, but it borrows several ideas from Java too. C# is object-oriented, which means your program is defined as a class, which is then instantiated through the Main() method call. To be able to draw on some of the .NET framework's many libraries, you need to add using statements at the top of your files — by default there's just using System; that enables you to get access to the console to write your message.

If you've come from C or C++, you'll notice that there are no header files in C#: your class definition and its implementation are all in the same file. You might also have noticed that the Main() method accepts the parameter 'string[] args', which is C#-speak for 'an array of strings.' C never had a native 'string' data type, whereas C++ acquired it rather late in the game, and so both languages tend to use the antiquated char* data type to point to a string of characters. In C#, 'string' is a data type all its own, and comes with built-in functionality such as the capability to replace substrings, the capability to trim off whitespace, and the capability to convert itself to upper- or lowercase if you want it to. Strings are also Unicode friendly out of the box in .NET, so that's one fewer thing for you to worry about.

The final thing you might have noticed — at least, if you had looked in the directory where MonoDevelop placed your compiled program (usually /path/to/your/project/bin/Debug) — is that Mono uses the Windows-like .exe file extension for its programs. because Mono aims to be 100% compatible with Microsoft .NET, which means you can take your Hello World program and run it unmodified on a Windows machine and have the same message printed out.

Printing Out the Parameters

We're going to expand your little program by having it print out all the parameters passed to it, one per line. In C#, this is — rather sickeningly — just one line of code. Add this just after the existing Console.WriteLine() line in your program:

foreach (string arg in args) Console.WriteLine(arg);

The foreach keyword is a special kind of loop designed to iterate over an array of finite size. The for array exists in C#, and lets you loop a certain number of times; the while array exists too, and lets you loop continuously until you tell it to break. But the foreach loop is designed to loop over arrays that have a specific number of values, but you don't know how many values that will be. You get each value as a variable of any type you want — the preceding code says string arg in args, which means 'for each array element in args, give it to me as the variable arg of type string. Of course, args is already an array of strings, so no datatype conversion will actually take place here — but it could if you wanted to convert classes or do anything of the like.

After you have each individual argument, call WriteLine() to print it out. This works whether there's one argument or one hundred arguments — or even if there are no arguments at all (in which case the loop doesn't execute at all).

Creating Your Own Variables

As you saw in the parameter list for Main() and the arg variable in your foreach loop, C# insists that each variable has a distinct data type. You can choose from quite a variety: Boolean for true/false values; string for text; int for numbers, float for floating-point numbers; and so on. If you want to be very specific, you can use int32 for a 32-bit integer (covering from -2147483648 to 2147483648) or int64 for a 64-bit integer (covering even larger numbers). But on the whole you can just use int and leave C# to work it out itself.

So now you can modify your program to accept two parameters, add them together as numbers, then print the result. This gives you a chance to see variable definitions, conversion, and mathematics, all in one. Edit the Main() method to look like this:

public static void Main (string[] args) {

 int num1 = Convert.ToInt32(args[0]);

 int num2 = Convert.ToInt32(args[1]);

 Console.WriteLine('Sum of two parameters is: ' + (num1 + num2)');

}

As you can see, each variable needs to be declared with a type (int) and a name (num1 and num2), so that C# knows how to handle them. Your

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату