1

来源:互联网 发布:单身久了 知乎 编辑:程序博客网 时间:2024/04/27 18:22

[ex01]

 class Class1
 {
  static void Main(string[] args)
  {
     Console.WriteLine("Hello, world");
     Console.Read();

 }
 } 

[result] Hello, world

 

[ex02] 数据类型VS引用类型

class Class1
 {
  static void Main(string[] args)
  {
   int val1 = 0;  //数据类型
   int val2 = val1;   val2 = 123; 
 
   Class2 ref1 = new Class2();
   Class2 ref2 = ref1;  //引用类型
   ref2.Value = 123;

   Console.WriteLine("Values: {0}, {1}", val1, val2);
   Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value);
   Console.Read();

  }
 }
 class Class2
 {
  public int Value = 0;
 }

[result] Values: 0, 123 
              Refs: 123, 123

[tips] 可以通过枚举和结构声明定义新数据类型,通过类、接口和代表声明来定义新引用类型。

 

[ex03]预定义类型

object 所有其它类型的最根本的基础类型object o = null;
string字符串类型;一个字符传是一个Unicode字符序列 string s = "Hello"; 
sbyte 8-bit 有符号整数类型 sbyte val = 12;
short 16-bit有符号整数类型short val = 12; 
int 32-bit 有符号整数类型 int val = 12;
long 64-bit有符号整数类型 long val1 = 12;  long val2 = 34L;
byte 8-bit 无符号整数类型 byte val1 = 12;  byte val2 = 34U;
ushort 16-bit无符号整数类型 ushort val1 = 12;  ushort val2 = 34U;
uint 32-bit无符号整数类型  uint val1 = 12; uint val2 = 34U;
ulong 64-bit无符号整数类型  ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 = 78UL;
float 单精度浮点数类型 float val = 1.23F;
double 双精度浮点数类型 double val1 = 1.23;  double val2 = 4.56D;
bool 二进制类型; 一个二进制数据不是真就是假bool val1 = true;   bool val2 = false;
char 字符类型; 一个字符数据是一个Unicode字符char val = 'h'; 
decimal精确十进制类型,有28个有效位decimal val = 1.23M;  

 class Class1
 {
  static void Main(string[] args)
  {
   string s = "Test";
   string t = string.Copy(s);
   Console.WriteLine(s == t);   // "Test"= "Test"
   Console.WriteLine((object)s == (object)t); // S<>T

   Console.Read();

  }
 }
[result] TRUE        FALSE

[EX04] ARRAY

  class Class1
 {
  static void Main(string[] args)
  {  int[] arr = new int[5];                //ARRAY
   for (int i = 0; i < arr.Length; i++)
    arr[i] = i * i;
   for (int i = 0; i < arr.Length; i++)
    Console.WriteLine("arr[{0}] = {1}", i, arr[i]);

   Console.Read();

  }
 }

[result]

arr[0] = 0
arr[1] = 1
arr[2] = 4
arr[3] = 9
arr[4] = 16

 class Test
{
  static void Main() { 
    int[ ] a1;      // single-dimensional array of int
    int[ , ] a2;    // 2-dimensional array of int
    int[ , , ] a3;  // 3-dimensional array of int
    int[ ][ ] j1;    // "jagged" array: array of (array of int)
    int[ ][ ][ ] j3; // array of (array of (array of int))
    int[ ][ ] j2 = new int[3][];
    j2[0] = new int[] {1, 2, 3};
    j2[1] = new int[] {1, 2, 3, 4, 5, 6};
    j2[2] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

 }
}

 

[EX05] 函数参数

class Test {
  static void F(int p) { //数据参数
  Console.WriteLine("p = {0}", p);
  p++;
 }
  static void Main() {
    int a = 1;
    Console.WriteLine("pre: a = {0}", a);
  F(a);
  Console.WriteLine("post: a = {0}", a);
 }
}

[RESULT]

pre: a = 1
p = 1
post: a = 1

class Test {
  static void Swap(ref int a, ref int b) { //引用参数
    int t = a;
  a = b;
  b = t;
 }
  static void Main() {
    int x = 1;
    int y = 2;
 
    Console.WriteLine("pre: x = {0}, y = {1}", x, y);
  Swap(ref x, ref y); //
    Console.WriteLine("post: x = {0}, y = {1}", x, y);
 }
}
[RESULT]
pre: x = 1, y = 2
post: x = 2, y = 1

 

 class Test {
  static void Divide(int a, int b, out int result, out int remainder) { //输出参数,一个是除的结果,另外一个是余数。
  result = a / b;
    remainder = a % b;
 }
  static void Main() {
    for (int i = 1; i < 10; i++)
      for (int j = 1; j < 10; j++) {
    int ans, r;
    Divide(i, j, out ans, out r); //
        Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r);
   }
 }
}

 class Test 
{
  static void F(params int[] args) { //
    Console.WriteLine("# of arguments: {0}", args.Length);
    for (int i = 0; i < args.Length; i++)
   Console.WriteLine("/targs[{0}] = {1}", i, args[i]);
 }
  static void Main() {
  F();
  F(1);
  F(1, 2);
  F(1, 2, 3);
    F(new int[] {1, 2, 3, 4});
 }
}

[RESULT]
# of arguments: 0
# of arguments: 1
  args[0] = 1
# of arguments: 2
  args[0] = 1
  args[1] = 2
# of arguments: 3
  args[0] = 1
  args[1] = 2
  args[2] = 3
# of arguments: 4
  args[0] = 1
  args[1] = 2
  args[2] = 3
  args[3] = 4

[EX06]自动内存管理

using System;
public class Stack
{
  private Node first = null;
  public bool Empty {
  get {
   return (first == null);
  }
 }
  public object Pop() {
  if (first == null) 
      throw new Exception("Can't Pop from an empty Stack.");
  else {
   object temp = first.Value;
   first = first.Next;
   return temp;
  }
 }
  public void Push(object o) {
    first = new Node(o, first);
 }
 class Node
 {
  public Node Next;
  public object Value;
    public Node(object value): this(value, null) {}
    public Node(object value, Node next) {
   Next = next;
   Value = value;
  }
 }
}

class Test
{
  static void Main() {
    Stack s = new Stack();
    for (int i = 0; i < 10; i++)
       s.Push(i);
  s = null;
 }
}

[RESULT]
Stack被创建并且初始化为包含10个元素,然后被赋值为数据null。一旦变量s被赋值为null后,Stack和相应的10个Node实例就变成符合碎片收集的条件了。碎片收集程序马上就被允许进行清理

[EX07]“非安全”代码

这样用unsafe修饰的代码可以由指针类型直接处理,而且fix对像可以暂时保护这些代码,防止被碎片收集程序收集。

class Test
{
  unsafe static void WriteLocations(byte[] arr) { //
    fixed (byte *p_arr = arr) { //
   byte *p_elem = p_arr;
      for (int i = 0; i < arr.Length; i++) {
    byte value = *p_elem;
        string addr = int.Format((int) p_elem, "X");
    Console.WriteLine("arr[{0}] at 0x{1} is {2}", i, addr, value);
    p_elem++;
   }
  }
 }
  static void Main() {
    byte[] arr = new byte[] {1, 2, 3, 4, 5};
  WriteLocations(arr);
 }
}
[RESULT]
arr[0] at 0x8E0360 is 1
arr[1] at 0x8E0361 is 2
arr[2] at 0x8E0362 is 3
arr[3] at 0x8E0363 is 4
arr[4] at 0x8E0364 is 5

[EX08]操作

基本的 (x)  x.y  f(x)  a[x]  x++  x--  new  typeof  sizeof  checked  unchecked
一元的 +  -  !  ~  ++x  --x  (T)x
乘法的 *  /  %
加法的 +  -
移位 <<  >>
关系 <  >  <=  >=  is
等式 ==  !=
逻辑与 &
逻辑异或 ^
逻辑或 |
条件与 &&
条件或 ||
条件的 ?:
赋值 =  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=

原创粉丝点击