框架
using System;
Console.WriteLine("Hello world!");
namespace YourNamespace
{
class YourClass
{
}
struct YourStruct
{
}
interface IYourInterface
{
}
delegate int YourDelegate();//委托
enum YourEnum //枚举
{
}
namespace YourNestedNamesapce
{
struct YourStruct
{
}
}
}
namespace Acme.Collections;
public class Stack<T> //Stack是泛型类,它具有一个类型参数T,在使用时替换为具体类型
{
Entry _top; //字段
public void Push(T data) //方法
{
_top = new Entry(_top,data);
}
public T Pop() //方法
{
if (_top == null)
{
throw new InvalidOperationExceptoin();
}
T result = _top.Data;
_top = _top.Next;
return result;
}
class Entry //嵌套类
{
public Entry Next {get;set;} //属性
public T Data {get;set;} //属性
public Entry(Entry next,T data) //构造函数
{
Next = next;
Data = data;
}
}
}
class Example
{
public static void Main()
{
var s= new Acme.Collections.Stack<int>();
s.Push(1);//入栈
s.Push(10);
s.push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
public class Pair<TFirst,TSecond> //泛型类,类型参数
{
public TFirst First { get;}
public TSecond Second {get;}
public Pair(TFirst first, TSecond second) => //构造泛型函数
(First,Second) = (first,second);
var pair = new Pair<int,string>(1,"two");
int i = pair.First;
string s = pair.Second;
}
基类、结构、接口、枚举、元组
//基类
public class Point3D : Point3D
{
public int Z { get; set;}
public Point3D(int x,int y,int Z):base(x,y)
{
Z = z;
}
Point a = new(10,20);
Point b = new Point3D(10,20,30);
}
//结构
public struct Point
{
pubic double X { get; }
pubic double Y { get; }
public Point(double x,double y) => (X, Y) = (x, y);
}
//接口
interface IControl
{
void Paint();
}
interface ITextBox : IControl
{
void SetText(string text);
}
interface IListBox : IControl
{
void SetItems(string[] items);
}
interface IComboBox : ITextBox,IListBox {}
}
interface IDataBound
{
void Bind(Binder b);
}
public class EditBox : IControl, IDataBound
{
public void Paint() { }
public void Bind(Binder b) { }
}
EditBox editBox = new();
IControl control = editBox;
IDataBound dataBound = editBox;
//枚举
public enum SomeRootVegetable
{
HorseRadish,
Radish,
Turnip
}
public enum Seasons
{
None = 0,
Summer = 1,
Autumn = 2,
Winter = 4,
Spring = 8,
All = Summer | Autumn | Winter | Spring
var turnip = SomeRootVegetable.Turnip;
var spring = Seasons.Spring;
var startingOnEquinox = Seasons.Spring | Seasons.Autumn;
var theYear = Seasons.All;
}
int? optionalInt = default;
optionalInt = 5;
string? optionalText = default;
optionalText = "Hello World.";
//元组
(double Sum,int Count) t2 = (4.5,3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}");
//字段
public class Color
{
public static readonly Color Black = new(0,0,0);
public static readonly Color White = new(255,255,255);
public static readonly Color Red = new(255,0,0);
public static readonly Color Green = new(0,255,0);
public static readonly Color Blue = new(0,0,255);
public byte R;
public byte G;
public byte B;
public Color(byte r,byte g,byte b)
{
R = r;
G = g;
B = b;
}
}
//方法
public override string ToString() => "This is an object"; //重写ToString()方法
//参数
static void Swap(ref int x,ref int y) //ref引用参数
{
int temp = x;
x = y;
y = temp;
}
public static void SwapExample()
{
int i = 1,j = 2;
Swap(ref i,ref j);
Console.WriteLine($"{i} {j}");
}
static void Divide(int x, int y, out int result, out int remainder)
{
result = x / y;
remainder = x % y;
}
public static void OutUsage()
{
Divide(10,3,out int res,out int rem); //输出参数,按引用传递自变量。与ref相似,不同之处在于,不要求向调用方提供自变量的显式赋值
Console.WriteLine($"{res} {rem}");
}
public class Console
{
public static void Write(string fmt,params object[] args) { }
public static void WriteLine(string fmt,params object[] args) { }
}
int x = 3, y = 4, z = 5;
string s = "x={0} y = {1} z={2}“;
object[] args = new object[3];
args[0] = x;
args[1] = y;
args[2] = z;
Console.WriteLine(s,args);
int x,y,z;
x =3;
y =4;
z = 5;
Console.WriteLine("X={0} y = {1} z ={2}",x,y,z);
//方法主体和局部变量
class Squares
{
public static void WriteSquares()
{
int i = 0;
int j;
while (i < 10)
{
j = i * i;
Console.WriteLines($"{i} * {i} = {j}");
i++;
}
}
}
//静态和实例方法
class Entity
{
static int s_nextSerialNo;
int _serialNo;
public Entity()
{
_serialNo = s_nextSerialNo++;
}
public int GetSerialNo()
{
return _serialNo;
}
public static int GetNextSerialNo()
{
return s_nextSerialNo;
}
public static void SetNextSeriaNo(int value)
{
s_nextSerialNo = value;
}
Entity.SetNextSeriaNo(1000);
Entity e1 = new();
Entity e2 = new();
Console.WriteLine(e1.GetSerialNo());//实例方法在类实例中进行调用
Console.WriteLine(e2.GetSerialNo());
Console.WriteLine(Entity.GetNextSerialNo());//静态方法在类中进行调用
}