分享
 
 
 

WHAT IS C#

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

What Is C#?

作者: 发表时间: 2003-7-13 10:22:21

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

来自: 阅读次数: 947

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有