这篇是几年前翻译的。原文约1.5万字,现在网上广泛流传的完全翻译版就是我这个,不过多数转载都已经看不到我的名字了,有的连Ikram的名字也没了。
在这里存个档。
© 版权所有 野比 2008
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# 的讨论主题:
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"); } } }
现在在你的 HelloWorld 类中你可以这样访问:
using System; using AnotherNameSpace; // 你可以增加这条语句 namespace MyNameSpace { class HelloWorld { static void Main(string[] args) { AnotherClass obj = new AnotherClass(); obj.Func(); } } }
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) { //.... }
int a= 5; FunctionA(ref a); // 使用 ref,否则将引发编译时错误 Console.WriteLine(a); // 打印 20[/code] [code]void FunctionA(ref int Val) { int x= Val; Val = x* 4; }
int Val; GetNodeValue(Val); bool GetNodeValue(out int Val) { Val = value; return true; }
void Func(params int[] array) { Console.WriteLine("number of elements {0}", array.Length); } Func(); // 打印 0 Func(5); // 打印 1 Func(7,9); // 打印 2 Func(new int[] {3,8,10}); // 打印 3 int[] array = new int[8] {1,3,4,5,5,6,7,5}; Func(array); // 打印 8
Shape shp =[/color] new Shape(); Vehicle veh = shp as Vehicle; // 返回 null,类型不可转换 Circle cir = new Circle(); Shape shp = cir; Circle cir2 = shp as Circle; //将进行转换 object[] objects = new object[2]; objects[0] = "Aisha"; object[1] = new Shape(); string str; for(int i=0; i&< objects.Length; i++) { str = objects[i] as string; if(str == null) Console.WriteLine("can not be converted"); else Console.WriteLine("{0}",str); }
Output: Aisha can not be converted
foreach (string s in array) Console.WriteLine(s);
int x = Int32.MaxValue; x++; // 溢出检查 { x++; // 异常 } unchecked { x++; // 溢出 } 下面的语句已修改:(野比译注:这句实属多余,原文如此,疑为作者笔误)
例子:
int var = 100; switch (var) { case 100: Console.WriteLine("<Value is 100>"); // 这里没有 break case 200: Console.WriteLine("<Value is 200>"); break; }
<Value is 100><Value is 200>
error CS0163: Control cannot fall through from one case label ('case 100:') to another
switch (var) { case 100: case 200: Console.WriteLine("100 or 200<VALUE is 200>"); break; }
const string WeekEnd = "Sunday"; const string WeekDay1 = "Monday"; //.... string WeekDay = Console.ReadLine(); switch (WeekDay ) { case WeekEnd: Console.WriteLine("It's weekend!!"); break; case WeekDay1: Console.WriteLine("It's Monday"); break; }
delegate int Operation(int val1, int val2); public int Add(int val1, int val2) { return val1 + val2; } public int Subtract (int val1, int val2) { return val1- val2; } public void Perform() { Operation Oper; Console.WriteLine("Enter + or - "); string optor = Console.ReadLine(); Console.WriteLine("Enter 2 operands"); string opnd1 = Console.ReadLine(); string opnd2 = Console.ReadLine(); int val1 = Convert.ToInt32 (opnd1); int val2 = Convert.ToInt32 (opnd2); if (optor == "+") Oper = new Operation(Add); else Oper = new Operation(Subtract); Console.WriteLine(" Result = {0}", Oper(val1, val2)); }
class Parent{ } class Child : Parent
class Shape { public virtual void Draw() { Console.WriteLine("Shape.Draw") ; } } class Rectangle : Shape { public override void Draw() { Console.WriteLine("Rectangle.Draw"); } } class Square : Rectangle { public override void Draw() { Console.WriteLine("Square.Draw"); } } class MainClass { static void Main(string[] args) { Shape[] shp = new Shape[3]; Rectangle rect = new Rectangle(); shp[0] = new Shape(); shp[1] = rect; shp[2] = new Square(); shp[0].Draw(); shp[1].Draw(); shp[2].Draw(); } } Output: Shape.Draw Rectangle.Draw Square.Draw
class Shape { public virtual void Draw() { Console.WriteLine("Shape.Draw") ; } } class Rectangle : Shape { public new void Draw() { Console.WriteLine("Rectangle.Draw"); } } class Square : Rectangle { //这里不用 override public new void Draw() { Console.WriteLine("Square.Draw"); } } class MainClass { static void Main(string[] args) { Console.WriteLine("Using Polymorphism:"); Shape[] shp = new Shape[3]; Rectangle rect = new Rectangle(); shp[0] = new Shape(); shp[1] = rect; shp[2] = new Square(); shp[0].Draw(); shp[1].Draw(); shp[2].Draw(); Console.WriteLine("Using without Polymorphism:"); rect.Draw(); Square sqr = new Square(); sqr.Draw(); } }
Output: Using Polymorphism Shape.Draw Shape.Draw Shape.Draw Using without Polymorphism: Rectangle.Draw Square.Draw
public Child(int val) :base(val) { myVar = 5; base.myVar; } //OR public Child(int val) { base(val); myVar = 5 ; base.myVar; }
© 版权所有 野比 2008