Time of Update: 2018-12-07
if (bool) { } else { }switch (v) { case v1: ... break; case v2: ... break; ... default: ... break; }do { } while (bool);while (bool) { }for (int i; i C# 的 switch 語句支援字串, 但好像只能用 const string 類型的變數.using System;class MyClass{ static void Main() {
Time of Update: 2018-12-07
顯示枚舉、枚舉值、枚舉名:using System;class MyClass{ enum MyEnum { AA, BB, CC }; /* 類型定義不能在函數體內 */ static void Main() { MyEnum e = MyEnum.CC; Console.WriteLine(e); // CC Console.WriteLine((byte)e); //
Time of Update: 2018-12-07
字串數組:using System;class MyClass{ static void Main() { string[] arr = new string[3] { "aa", "bb", "cc" }; foreach (string s in arr) Console.WriteLine(s); // aa/bb/cc Console.ReadKey(); }}整數數組:using System;class MyClass{
Time of Update: 2018-12-07
無參、無傳回值的函數:using System;class MyClass{ static void Show() { Console.WriteLine("function"); } static void Main() { Show(); //function Console.ReadKey(); }}參數:using System;class MyClass{ static void
Time of Update: 2018-12-07
所有類預設繼承於 System.Object(或叫 Object):using System;class MyClass1{}class MyClass2 : Object{}class MyClass3 : System.Object{}class Program{ static void Main() { MyClass1 obj1 = new MyClass1(); MyClass2 obj2 = new MyClass2();
Time of Update: 2018-12-07
1. 傳值參數(非指標參數):#include <stdio.h>int inc(int x);int main(void){ int num = 1; printf("%d\n", inc(num)); /* 2 */ printf("%d\n", num); /* 1; num 並沒有改變, 用作函數參數時只是複製過去 */ getchar(); return 0;}int inc(int x) { x++;
Time of Update: 2018-12-07
1. 局部變數:局部變數也叫自動變數, 它聲明在函數開始, 生存於棧, 它的生命隨著函數返回而結束.#include <stdio.h>int main(void){ auto int i = 9; /* 聲明局部變數的關鍵字是 auto; 因可以省略, 幾乎沒人使用 */ printf("%d\n", i); getchar(); return 0;}2. 全域變數:全域變數聲明在函數體外, 一般應在函數前; 每個函數都可以使用它,
Time of Update: 2018-12-07
數組參數屬於指標參數.指標參數即時傳址參數(或叫引用參數), 如果想在函數中修改參數的值, 這是唯一的途徑.如果把數組當作參數, 不管你願意與否, 它就是指標, 指向第一個值的指標.1. 數組參數就是指向第一個元素的指標:#include <stdio.h>void getArr(int p[], int si);int main(void){ int ns[] = {1,2,3,4,5}; getArr(ns, sizeof(ns)/sizeof(ns[0]));
Time of Update: 2018-12-07
像 printf 這種多參函數, 是借用 stdarg.h 中的宏實現的.va_list : 用於定義遍曆參數列表的指標;va_start : 讓指標指向第一個參數;va_arg : 擷取下一個參數, 並向後移動一個位置;va_end : 釋放指標, 完成遍曆.1. 整數求和:本例實現了對系列整數求和, 要求至少要有三個參數, 並且最後一個必須是 0.最後的 0 用於識別列表結束.#include <stdio.h>#include <stdarg.h>int
Time of Update: 2018-12-07
非指標參數(也就是傳值參數)不會被修改原始值, const 對它是沒有意義的.const 只用於指標.1. 第一種用法: const 類型 *變數:這種用法將限制修改指標指向的值.#include <stdio.h>int fun(const int *p) { *p += 1; /* 只有去掉 const 這句才可以執行 */ return *p;}int main(void){ int num = 3; printf("%d\n", fun(&
Time of Update: 2018-12-07
先複習函數的定義與函數的聲明://這是一個求和函數的定義:int add(int x, int y){ return(x + y);}//可以這樣聲明:int add(int x, int y);//也可以這樣聲明:int add(int, int);定義一個函數指標聲明一個函數差不多, 用 (* ) 包括函數即可://像這樣:int (*pfun)(int, int);//或這樣:int (*pfun)(int x, int y);//也可以:typedef int
Time of Update: 2018-12-07
本例:代碼:using System;class MyClass{ static void Main() { Console.WriteLine("sbyte 類型:"); sbyte sbyteVar = (sbyte.MaxValue + sbyte.MinValue) / 2; Console.WriteLine("{0}、{1}、{2}\n", sbyte.MinValue, sbyteVar, sbyte.MaxValue);
Time of Update: 2018-12-07
繼承:using System;class Parent{ public void Msg() { Console.WriteLine("Parent"); }}class Child : Parent { }class Program{ static void Main() { Parent ObjParent = new Parent(); Child ObjChild = new Child(); ObjParent.Msg();
Time of Update: 2018-12-07
建構函式與解構函式:using System;class MyClass{ private int FNum; public int Num { get { return FNum; } } /* 建構函式沒有傳回值, 無參的建構函式是預設的 */ public MyClass() { this.FNum = 2009; } /* 可以有多個參數不同的建構函式 */ public MyClass(int x) {
Time of Update: 2018-12-07
說實話, 有了 Delphi 真的很難喜歡 C++; 起碼對我是這樣.在我學點 C 與 C++ 的同時, 更加感到 Delphi 的優秀: 它就是 C 與 C++ 集合、與進步.但當 Delphi 過了入門關以後, 學習資料實在是太少了!很多好的技術是用 C/C++ 描述的... 因此我不得不硬著頭皮去瞭解一些.起初僅僅是為了更好地看懂那些書, 但也有意外收穫: 換個視角看 Delphi、能更好地理解 Delphi.但我不認為這是繼續深入 Delphi 的好方法, 實在是別無選擇;
Time of Update: 2018-12-07
抽象類別不能直接執行個體化:using System;abstract class MyClass{ }class Program{ static void Main() { /* 抽象類別不能直接執行個體化, 下面這樣會出錯 */ MyClass obj = new MyClass(); Console.ReadKey(); }}但抽象類別可以通過子類執行個體化:using System;abstract class
Time of Update: 2018-12-07
基本: . () [] x++ x-- new typeof checked unchecked -> ::一元: + - ! ~ ++x --x (T)x True False & sizeof乘除: * / %加減: + -移位: >關係: = is as 相等: == !=邏輯: & ^ |條件: && ||賦值: = += -= *= /= %= &= |=
Time of Update: 2018-12-07
通過索引器可以方便使用類中的數組(或集合)成員:using System;class MyClass{ private float[] fs = new float[3] { 1.1f, 2.2f, 3.3f }; /* 屬性 */ public int Length { get { return fs.Length; } set { fs = new float[value]; } } /* 索引器 */ public
Time of Update: 2018-12-07
使用 Convert 類:ToBoolean -> boolToByte -> byteToChar -> charToDateTime -> DateTimeToDecimal -> decimalToDouble -> doubleToInt16 -> shortToInt32 -> intToInt64 -> longToSByte -> sbyteToSingle ->
Time of Update: 2018-12-07
using System;class MyClass{ private int FNum; public delegate void MyDelegate(int n); /* 委託是事件的前提 */ public event MyDelegate MyEvent; /* 用 event 關鍵字根據已有的委託聲明事件 */ /* 假如是在給 Num 賦值時觸動事件 */ public int Num { get { return