Книга: Fedora™ Unleashed, 2008 edition

The Structure of a C# Program

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.

Оглавление книги


Генерация: 1.881. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз