What IS C#?

KK4 2003-10-09 02:54:05
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





...全文
78 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
KK4 2003-10-10
  • 打赏
  • 举报
回复
大家選擇學習語言之前應該先了解該語言的基本優缺點!
nxct 2003-10-09
  • 打赏
  • 举报
回复
c# = c++++
:)
HunterForPig 2003-10-09
  • 打赏
  • 举报
回复
up
JoeM 2003-10-09
  • 打赏
  • 举报
回复
suck
张张好 2003-10-09
  • 打赏
  • 举报
回复
good
6HZ 2003-10-09
  • 打赏
  • 举报
回复
what's the class?
what's the inherit and derive?
what's the multi-state?
KK4 2003-10-09
  • 打赏
  • 举报
回复
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

--------------------------------------------------------------------------------

6HZ 2003-10-09
  • 打赏
  • 举报
回复
KK4(逐月),
please continue...
mancatfg 2003-10-09
  • 打赏
  • 举报
回复
C#是一种简单、现代、面向对象和类型安全的编程语言,由C和C++发展而来。C#(发音为“C霎普”)牢固地植根于C和C++语言族谱中,并且会很快被C和C++程序员所熟悉。C#的目标在于把Visual Basic的高生产力和C++本身的能力结合起来。
C#作为Microsoft Visual Studio 7.0的一部分提供给用户。除了C#以外,Visual Studio还支持Visual Basic、Visual C++和描述语言VBScript和Jscript。所有这些语言都提供对Microsoft .NET平台的访问能力,它包括一个通用的执行引擎和一个丰富的类库。Microsoft .NET平台定义了一个“通用语言子集”(CLS),是一种混合语言,它可以增强CLS兼容语言和类库间的无缝协同工作能力。对于C#开发者,这意味着既是C#是一种新的语言,它已经可以对用老牌工具如Visual Basic和Visual C++使用的丰富类库进行完全访问。C#自己并没有包含一个类库。
wyz52126 2003-10-09
  • 打赏
  • 举报
回复
C#的目标在于把Visual Basic的高生产力和C++本身的能力结合起来??
6HZ 2003-10-09
  • 打赏
  • 举报
回复
Good,thanks!
I'm studying the C# and dot net framework.
feathersea 2003-10-09
  • 打赏
  • 举报
回复
C#是一种简单、现代、面向对象和类型安全的编程语言,由C和C++发展而来。C#(发音为“C霎普”)牢固地植根于C和C++语言族谱中,并且会很快被C和C++程序员所熟悉。C#的目标在于把Visual Basic的高生产力和C++本身的能力结合起来。
C#作为Microsoft Visual Studio 7.0的一部分提供给用户。除了C#以外,Visual Studio还支持Visual Basic、Visual C++和描述语言VBScript和Jscript。所有这些语言都提供对Microsoft .NET平台的访问能力,它包括一个通用的执行引擎和一个丰富的类库。Microsoft .NET平台定义了一个“通用语言子集”(CLS),是一种混合语言,它可以增强CLS兼容语言和类库间的无缝协同工作能力。对于C#开发者,这意味着既是C#是一种新的语言,它已经可以对用老牌工具如Visual Basic和Visual C++使用的丰富类库进行完全访问。C#自己并没有包含一个类库。
sarmoo 2003-10-09
  • 打赏
  • 举报
回复
up
KK4 2003-10-09
  • 打赏
  • 举报
回复
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:
KK4 2003-10-09
  • 打赏
  • 举报
回复
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++.
KK4 2003-10-09
  • 打赏
  • 举报
回复
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.
Paperback: 1625 pages Publisher: Apress; 7th ed. 2015 edition (January 1, 2016) Language: English ISBN-10: 1484213335 ISBN-13: 978-1484213339 This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including: A Refined ADO.NET Entity Framework Programming Model Numerous IDE and MVVM Enhancements for WPF Desktop Development Numerous updates to the ASP.NET Web APIs This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies). The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms. What you’ll learn Be the first to understand the .NET 4.6 platform and C# 6. Discover the ins and outs of the leading .NET technology. Learn from an award-winning author who has been teaching the .NET world since version 1.0. Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime. Who this book is for This book is perfect for anyone who is interested in the new .NET Framework 4.6 and the C# language. Whether you are moving to .NET for the first time or are already writing applications using previous .NET versions, this book will provide you with a comprehensive grounding in the new technology and serve as a complete reference throughout your coding career.

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧