CSDN博客

img shaplong9000

What Is C#?

发表于2004/10/4 16:48:00  580人阅读

分类: .NET

C# is the new modern language for developing applications for Microsoft's .NET platform. This document will go through the basics of this language
and explain how you can use it to write efficient platform neutral applications. The .NET platform is designed to be language neutral, indeed, all .NET code is run under the Common Language Runtime (CLR). C# is
just one of the languages that can be used to write classes for the CLR, but so far it is the only language that was written from the outset with .NET in mind. This makes C# as the language of choice for .NET development.

-----------------------------------
What is C#?
-----------------------------------
The most simple C# program is shown here:

// in Simple.cs

class App
{
  public static void Main()
  {

  }

}



This code does nothing, but will compile fine using
the C# compiler csc.exe:

csc Simple.cs

Before making this application do more let me explain the code that you already seen. The code defines a .NET class which has a single public method. By public I mean that the method can be called from code outside of the class, in general a method marked as private is only accessible from code in the current
class. However Main() is a special case, you can declare it as private and the system will still be able to call it, but in practice you should always declare it as public.

The method is also marked as static. Classes are usually used to create objects – instances of the class – and methods are called through objects using data that is specific to an object. However, a static method can be called without an object instance. .NET does not allow you to write global methods, every method has to be part of a class, so static is the only way to call a method without first creating an object.

Because static methods are not associated with objects, they cannot call non-static methods or use other members of the class that is non-static. For example:

class App
{
public static void Main()
{
f(); // this will not compile!
}
public void f()
{
}
}

One typical reason for a static method is to create an instance of the class, this is done in C# with the new operator:

class App
{
public static void Main()
{
App app = new App();
app.f();
}
public void f()
{
}
}

The two new lines create an instance of the App class and call the non-static method f() through that instance; app is known as a reference. The syntax used here is different to what you would expect in C++.
Firstly, the code must explicitly specify the constructor that will be used (in this case the default constructor that is provided by C#). The second difference is that although new is used to create a reference there is no equivalent of the C++ delete operator. The reason is that .NET has a garbage collector that monitors object usage and when all references to an object have been released (or if there is a circular dependency) the garbage collector can release the object.

In C++ new has the specific meaning of “create a new instance of this class in the C++ free store”. The meaning of new in C# is more wide ranging, it merely says “create a new instance of this class”, usually this class will be created on the heap and managed by the .NET garbage collector, but if used to create an instance of a value type (a term that will be explained later) the object will be created on the stack.

--------------------------------------------
Note: if you have a class that holds resources that should not be held for a long time (for example an exclusive lock on a file), then you should implement a method on the class to free this resource and explicitly call this method when you are sure that the object will not longer be used. Typically , such a method is called Dispose().
--------------------------------------------
The Main() function has a special meaning, it is the entry point for an application, and every C# application must have a class with a public static Main() method - and only one such class. As in C++, the Main() method may return void or an int and it can take parameters. Main() that accepts command line parameters looks like this:

public static int Main(string[] args)
{
return 0;
}

The args parameter is an array of strings. Arrays in .NET are instances of the System.Array class, and items are accessed through square brackets, as in C++.
The size of an array is determined by accessing the Length property. A property gives access to data in the class.

For your application to do anything it must use a class library. A class library contains classes that perform various system actions like creating and manipulating windows, managing security, or accessing databases. C# code uses the .NET library that is common to all code that runs on the .NET platform.
This means that the same library is available to VB.NET code and through the Managed Extensions for C++.

A class library is accessed through the using keyword:

// in Simple.cs

using System;

class App
{
public static int Main(string[] args)
{
for(int x = 0; x < args.Length; x++)
Console.WriteLine(args[x]);
return 0;
}
}

Here, the using line indicates that the code will use classes defined in the System namespace. A namespace is a collection of .NET classes, which are usually related. As you get more proficient with C# you'll create your own namespaces and putting classes in a namespace allows other applications to use your classes. This code uses a class called Console. As the name suggests this gives access to the command line console. This simple example prints all the command line arguments to the console. Namespaces scope class names, using System in System.String means that you indicate that you want to use String from the System namespace as opposed to String defined in another namespace. You use the /reference switch on csc to indicate the namespaces that your code will use. The using keyword allows you to use classes without using the fully qualified name.

Note: like C++ and C, each statement must end in a semicolon. However, unlike C++ the class declaration does not have to have a terminating semicolon.

C# allows methods to be overloaded. What this means is that a class can have methods with the same name, but that take different parameters. For example, you can write the following for Main():

public static void Main()
{
int i = 1;
double f = 2.0;
Console.WriteLine("Integer");
Console.WriteLine(i);
Console.WriteLine("Floating point");
Console.WriteLine(f);
}

Other than literal strings, this code passes an integer and a double precision floating point number to WriteConsole(). The output of this new program is:

Integer

1
Floating point

2
Notice that there is a newline after each call to WriteLine(). There are two ways to get the string and value on the same line, both are shown in this next code:

public static void Main()
{
int i = 1;
double f = 2.0;
Console.Write("Integer ");
Console.WriteLine(i);
Console.WriteLine("Floating point {0}", f);
}

In the first case I have used the Write() method instead of the WriteLine() method to write the literal string. In the second case I have used another overload of WriteLine() that takes a format string and a variable number of parameters. The format string has placeholders indexed from 0, each one identified by braces ({}). The runtime library will replace the placeholders with the actual values in the variables. The index of the placeholder reflects the order of the parameters to WriteLine(), so the following will print out the integer first, and then the floating point number:

Console.WriteLine("Integer {1} Floating point {0}", f, i);

What about formatting options? Well, the common class library comes with classes to allow you to format numbers, which I will cover later. However, Write() and WriteLine() does support column formatting. The syntax is shown in the following example:
Console.WriteLine("{0,-10}{1,-3}", "Name","Age");

Console.WriteLine("-------------");

Console.WriteLine("{0,-10}{1,3}", "Richard", richardsAge);
Console.WriteLine("{0,-10}{1,3}", "Jenny", jennysAge);

Here, the second number in the braces specifies the width of the column and the justification. -10 means that the Name column is 10 characters wide and items are left justified, a value of 10 will make the items right justified. The output from the lines above will be:

Name Age
-------------
Richard 36
Jenny 8
阅读全文
0 0

相关文章推荐

img
取 消
img