「学一学」C# 快速入门(Aisha Ikram著,野比 译)
这篇是几年前翻译的。原文约1.5万字,现在网上广泛流传的完全翻译版就是我这个,不过多数转载都已经看不到我的名字了,有的连Ikram的名字也没了。
© 版权所有 野比 2008
各位看我的帖子代码吃力的朋友请先耐心把这篇文章读完,对C#有个大概印象先。
----------------------
电子版(PDF)下载
C# 快速入门
[英]Aisha Ikram 著 野比 译
来源:www.codeproject.com(Quick C#)
译注:原文极长(约 1.5 万字),但浅显易懂,讲解生动活泼,请耐心阅读
在帖子里阅读做好心理准备,极长
在一小时内学会 C#。使用例程,简单却完整的探索 C# 语言的构造和特点。本文特别适合有 C++ 基础却没有太多精力学习 C# 的读者。
© 版权所有 野比 2008
关于作者
Aisha Ikram
我现在在英国一家软件公司任技术带头人。我是计算机科学的硕士。我主要使用 .NET 1.1/2.0, C#, VB.NET, ASP.NET, VC++ 6, MFC, ATL, COM/DCOM, SQL Server 2000/2005等。最近我在学习 .NET 3.x 的全部内容。我的免费源代码和文章网站是 http://aishai.netfirms.com
职业:团队带头人
位置:英国
简介
C# 是一种具有 C++ 特性,Java 样式及 BASIC 快速建模特性的编程语言。如果你已经知晓 C++ 语言,本文将在不到一小时的时间内带你快速浏览 C# 的语法。如果熟悉 Java 语言,Java 的编程结构、打包和垃圾回收的概念肯定对你快速学习 C# 大有帮助。所以我在讨论 C# 语言构造的时候会假设你知道 C++。
本文通过一系列例程以简短但全面的方式讨论了 C# 语言构造和特性,所以你仅需略览代码片刻,即可了解其概念。
注意:本文不是为 C# 宗师而写。有很多初学者的 C# 文章,这只是其中之一。
接下来关于 C# 的讨论主题:
编程结构
命名空间
数据类型
变量
运算符与表达式
枚举
语句
类与结构
修饰符
属性
接口
函数参数
数组
索引器
装箱与拆箱
委托
继承与多态
以下主题不会进行讨论:
C++ 与 C# 的共同点
诸如垃圾回收、线程、文件处理等概念
数据类型转换
异常处理
.NET 库
编程结构
和 C++ 一样,C# 是大小写敏感的。半角分号(;)是语句分隔符。和 C++ 有所区别的是,C# 中没有单独的声明(头)和实现(CPP)文件。所有代码(类声明和实现)都放在扩展名为 cs 的单一文件中。
看看 C# 中的 Hello World 程序。
using System;namespace MyNameSpace{class HelloWorld{ static void Main(string[] args) { Console.WriteLine ("Hello World"); }}}
using System;namespace AnotherNameSpace{ class AnotherClass { public void Func() { Console.WriteLine ("Hello World"); } }}
using AnotherNameSpace; // 你可以增加这条语句namespace MyNameSpace{class HelloWorld{ static void Main(string[] args) { AnotherClass obj = new AnotherClass(); obj.Func(); }}}
long 8 signed long
ulong 8 unsigned long
float 4 floating point number
double 8 double precision number
decimal 8 fixed precision number
string - Unicode string
char - Unicode char
bool true, false boolean
注意:C# 的类型范围和 C++ 不同。例如:long 在 C++ 中是 4 字节而在 C# 中是 8 字节。bool 和 string 类型均和 C++ 不同。bool 仅接受真、假而非任意整数。
用户定义类型文件包含:
类 (class)
结构(struct)
接口(interface)
以下类型继承时均分配内存:
值类型
参考类型
值类型
值类型是在堆栈中分配的数据类型。它们包括了:
除字符串,所有基本和内建类型
结构
枚举类型
引用类型
引用类型在堆(heap)中分配内存且当其不再使用时,将自动进行垃圾清理。和 C++ 要求用户显示创建 delete 运算符不一样,它们使用新运算符创建,且没有 delete 运算符。在 C# 中它们自动由垃圾回收系统回收。
引用类型包括:
类
接口
集合类型如数组
字符串
枚举
C# 中的枚举和 C++ 完全一样。通过关键字 enum 定义。
例子:
enum Weekdays{ Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday}
struct Date{ int day; int month; int year;} class Date{ int day; int month; int year; string weekday; string monthName; public int GetDay() { return day; } public int GetMonth() { return month; } public int GetYear() { return year; } public void SetDay(int Day) { day = Day ; } public void SetMonth(int Month) { month = Month; } public void SetYear(int Year) { year = Year; } public bool IsLeapYear() { return (year/4 == 0); } public void SetDate (int day, int month, int year) { } //...}
using System;class Date{ public int Day{ get { return day; } set { day = value; } } int day; public int Month{ get { return month; } set { month = value; } } int month; public int Year{ get { return year; } set { year = value; } } int year; public bool IsLeapYear(int year) { return year%4== 0 ? true: false; } public void SetDate (int day, int month, int year) { this.day = day; this.month = month; this.year = year; }}
class User{ public static void Main() { Date date = new Date(); date.Day = 27; date.Month = 6; date.Year = 2003; Console.WriteLine ("Date: {0}/{1}/{2}", date.Day, date.Month, date.Year); }}
class MyClass{ const int constInt = 100; //直接进行 readonly int myInt = 5; //直接进行 readonly int myInt2; public MyClass() { myInt2 = 8; //间接进行 } public Func() { myInt = 7; //非法 Console.WriteLine(myInt2.ToString()); }}
sealed class CanNotbeTheParent{ int a = 5;}
public unsafe MyFunction( int * pInt, double* pDouble){ int* pAnotherInt = new int; *pAnotherInt = 10; pInt = pAnotherInt; ... *pDouble = 8.9; }
using System;interface myDrawing{ int originx { get; set; } int originy { get; set; } void Draw(object shape); }class Shape: myDrawing{ int OriX; int OriY; public int originx { get{ return OriX; } set{ OriX = value; } } public int originy { get{ return OriY; } set{ OriY = value; } } public void Draw(object shape) { //... // 做要做的事 } // 类自身的方法 public void MoveShape(int newX, int newY) { //..... } }
int[] array = new int[10]; // int 型一维数组for (int i = 0; i < array.Length; i++) array[i] = i; int[,] array2 = new int[5,10]; // int 型二维数组array2[1,2] = 5;int[,,] array3 = new int[5,10,5]; // int 型三维数组array3[0,2,4] = 9;int[][] arrayOfarray = new int[2]; // int 型交错数组 - 数组的数组arrayOfarray[0] = new int[4]; arrayOfarray[0] = new int[] {1,2,15};
class Shapes: CollectionBase { public void add(Shape shp) { List.Add(shp); } //indexer public Shape this[int index] { get { return (Shape) List[index]; } set { List[index] = value ; } }}
class Test{ static void Main() { int myInt = 12; object obj = myInt ; // 装箱 int myInt2 = (int) obj; // 拆箱 }}
SetDay([/color]5);//...void SetDay(int day) { //....}
[解决办法]
还有很多东西用不着。。
[解决办法]
好东西,好东西,好东西,好东西。。。。
[解决办法]
大雄厉害
[解决办法]
不错。。。
[解决办法]
最近看C#入门经典(第五版),不错
[解决办法]
感谢分享,买书感觉太贵,并且感觉自己可能看不完就浪费了,还是下载资料看起来方便,想看就看,再次感谢楼主
[解决办法]
学习学习,我是学C#的但工作都没用他
[解决办法]