0
對象(Object)類型
對象(Object)類型 是 C# 通用類型系統(Common Type System - CTS)中所有數據類型的終極基類。Object 是 System.Object 類的別名。所以對象(Object)類型可以被分配任何其他類型(值類型、引用類型、預定義類型或用戶自定義類型)的值。但是,在分配值之前,需要先進行類型轉換。
當一個值類型轉換爲對象類型時,則被稱爲 裝箱;另一方面,當一個對象類型轉換爲值類型時,則被稱爲 拆箱。
object obj;
obj = 100; // 這是裝箱
動態(Dynamic)類型
您可以存儲任何類型的值在動態數據類型變量中。這些變量的類型檢查是在運行時發生的。
聲明動態類型的語法:
dynamic <variable_name> = value;
dynamic d = 20;
1
Console.ReadKey();
Console.ReadLine():
readline 按enter結束
readkey 按任意非功能鍵(字符) 結束
2
& | ^ 與或非
~ 按位取反運算符是一元運算符,具有"翻轉"位效果,即0變成1,1變成0,包括符號位。
<< 二進制左移運算符。左操作數的值向左移動右操作數指定的位數。
>> 二進制右移運算符。左操作數的值向右移動右操作數指定的位數。
3
Protected:只有該類對象及其子類對象可以訪問
Internal:同一個程序集的對象可以訪問;
Protected internal:該程序集內的派生類訪問,是protected和internal的交集;
4
ref
則方法定義和調用方法都必須顯式使用ref關鍵字。ref是 Reference的縮寫。
5
out
使用 輸出參數 來從函數中返回兩個值。輸出參數會把方法輸出的數據賦給自己,其他方面與引用參數相似。
public void getValue(out int x )
{
int temp = 5;
x = temp;
}
int a = 100;
n.getValue(out a);
6
可空類型(Nullable)
可空類型可以表示其基礎值類型正常範圍內的值,再加上一個 null 值。
< data_type> ? <variable_name> = null;
Null 合併運算符( ?? )
如果第一個操作數的值爲 null,則運算符返回第二個操作數的值,否則返回第一個操作數的值。
double? num1 = null;
num3 = num1 ?? 5.34;
Console.WriteLine("num3 的值: {0}", num3);
-----------num3 的值: 5.34
7
聲明數組
datatype[] arrayName;
double[] balance;
double[] balance = new double[10];
balance[0] = 4500.0;
int [] marks = new int[5] { 99, 98, 92, 97, 95};
使用 foreach 循環
在前面的實例中,我們使用一個 for 循環來訪問每個數組元素。您也可以使用一個 foreach 語句來遍歷數組。
int [] n = new int[10]; /* n 是一個帶有 10 個整數的數組 */
/* 初始化數組 n 中的元素 */
for ( int i = 0; i < 10; i++ )
{
n[i] = i + 100;
}
/* 輸出每個數組元素的值 */
foreach (int j in n )
{
int i = j-100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
j是從n中取得的值,第一個是100,第二個是101.。。第十個是109
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
8
using System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//字符串,字符串連接
string fname, lname;
fname = "Rowan";
Console.WriteLine("Full Name: {0}", fullname);
//通過使用 string 構造函數
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用於轉化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
waiting);
Console.WriteLine("Message: {0}", chat);
Console.ReadKey() ;
}
}
}
顯示
Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 17:58 on Wednesday, 10 October 2012
9
結構體與類
1、類是引用類型,結構是值類型。(既然class是引用類型,class可以設爲null。但是我們不能將struct設爲null,因爲它是值類型。)
2、結構不支持繼承。
3、結構不能聲明默認的構造函數。
10
enum 枚舉
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
默認情況下,第一個枚舉符號的值是 0.
Days.Mon 1
Days.Fri 5
11
類的 構造函數 是類的一個特殊的成員函數,當創建類的新對象時執行。
static 關鍵字把類成員定義爲靜態的。當我們聲明一個類成員爲靜態時,意味着無論有多少個類的對象被創建,只會有一個該靜態成員的副本。
也可以把一個成員函數聲明爲 static。這樣的函數只能訪問靜態變量。靜態函數在對象被創建之前就已經存在。
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public static int getNum()
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("變量 num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
變量 num: 3
12 繼承
基類 Person 和派生類 Employee 都有一個名爲 Getinfo 的方法。通過使用 base 關鍵字,可以從派生類中調用基類的 Getinfo 方法。
using System;
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
本示例顯示如何指定在創建派生類實例時調用的基類構造函數。
using System;
public class BaseClass
{
int num;
public BaseClass()
{
Console.WriteLine("in BaseClass()");
}
public BaseClass(int i)
{
num = i;
Console.WriteLine("in BaseClass(int i)");
}
public int GetNum()
{
return num;
}
}
public class DerivedClass : BaseClass
{
// This constructor will call BaseClass.BaseClass()
public DerivedClass() : base()
{
}
// This constructor will call BaseClass.BaseClass(int i)
public DerivedClass(int i) : base(i)
{
}
static void Main()
{
DerivedClass md = new DerivedClass();
DerivedClass md1 = new DerivedClass(1);
}
}
13
abstract class是抽象類.比如說,圓形和三角形都是形狀.圓形和三角形有具體的表現方式,可以說是具體類.而形狀沒辦法具體表現出來,就是抽象類.抽象類可以有自己的屬性,可以有非abstract 的方法.
請注意,下面是有關抽象類的一些規則:
您不能創建一個抽象類的實例。
您不能在一個抽象類外部聲明一個抽象方法。
通過在類定義前面放置關鍵字 sealed,可以將類聲明爲密封類。當一個類被聲明爲 sealed 時,它不能被繼承。抽象類不能被聲明爲 sealed。
當有一個定義在類中的函數需要在繼承類中實現時,可以使用虛方法。虛方法是使用關鍵字 virtual 聲明的。虛方法可以在不同的繼承類中有不同的實現。對虛方法的調用是在運行時發生的。
動態多態性是通過 抽象類 和 虛方法 實現的。
interface是接口.相當於某些具體對象的交叉點,這個點本身不能做什麼,只能把操作傳達給各個具體的對象,讓對象自己去執行.接口的數據成員必須是靜態且不能修改的(static final),所有的方法都是虛方法.
對象(Object)類型
對象(Object)類型 是 C# 通用類型系統(Common Type System - CTS)中所有數據類型的終極基類。Object 是 System.Object 類的別名。所以對象(Object)類型可以被分配任何其他類型(值類型、引用類型、預定義類型或用戶自定義類型)的值。但是,在分配值之前,需要先進行類型轉換。
當一個值類型轉換爲對象類型時,則被稱爲 裝箱;另一方面,當一個對象類型轉換爲值類型時,則被稱爲 拆箱。
object obj;
obj = 100; // 這是裝箱
動態(Dynamic)類型
您可以存儲任何類型的值在動態數據類型變量中。這些變量的類型檢查是在運行時發生的。
聲明動態類型的語法:
dynamic <variable_name> = value;
dynamic d = 20;
1
Console.ReadKey();
Console.ReadLine():
readline 按enter結束
readkey 按任意非功能鍵(字符) 結束
2
& | ^ 與或非
~ 按位取反運算符是一元運算符,具有"翻轉"位效果,即0變成1,1變成0,包括符號位。
<< 二進制左移運算符。左操作數的值向左移動右操作數指定的位數。
>> 二進制右移運算符。左操作數的值向右移動右操作數指定的位數。
3
Protected:只有該類對象及其子類對象可以訪問
Internal:同一個程序集的對象可以訪問;
Protected internal:該程序集內的派生類訪問,是protected和internal的交集;
4
ref
則方法定義和調用方法都必須顯式使用ref關鍵字。ref是 Reference的縮寫。
5
out
使用 輸出參數 來從函數中返回兩個值。輸出參數會把方法輸出的數據賦給自己,其他方面與引用參數相似。
public void getValue(out int x )
{
int temp = 5;
x = temp;
}
int a = 100;
n.getValue(out a);
6
可空類型(Nullable)
可空類型可以表示其基礎值類型正常範圍內的值,再加上一個 null 值。
< data_type> ? <variable_name> = null;
Null 合併運算符( ?? )
如果第一個操作數的值爲 null,則運算符返回第二個操作數的值,否則返回第一個操作數的值。
double? num1 = null;
num3 = num1 ?? 5.34;
Console.WriteLine("num3 的值: {0}", num3);
-----------num3 的值: 5.34
7
聲明數組
datatype[] arrayName;
double[] balance;
double[] balance = new double[10];
balance[0] = 4500.0;
int [] marks = new int[5] { 99, 98, 92, 97, 95};
使用 foreach 循環
在前面的實例中,我們使用一個 for 循環來訪問每個數組元素。您也可以使用一個 foreach 語句來遍歷數組。
int [] n = new int[10]; /* n 是一個帶有 10 個整數的數組 */
/* 初始化數組 n 中的元素 */
for ( int i = 0; i < 10; i++ )
{
n[i] = i + 100;
}
/* 輸出每個數組元素的值 */
foreach (int j in n )
{
int i = j-100;
Console.WriteLine("Element[{0}] = {1}", i, j);
}
j是從n中取得的值,第一個是100,第二個是101.。。第十個是109
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
8
using System;
namespace StringApplication
{
class Program
{
static void Main(string[] args)
{
//字符串,字符串連接
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
string fullname = fname + lname;Console.WriteLine("Full Name: {0}", fullname);
//通過使用 string 構造函數
char[] letters = { 'H', 'e', 'l', 'l','o' };
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
//方法返回字符串
string[] sarray = { "Hello", "From", "Tutorials", "Point" };
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
//用於轉化值的格式化方法
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}",
waiting);
Console.WriteLine("Message: {0}", chat);
Console.ReadKey() ;
}
}
}
顯示
Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 17:58 on Wednesday, 10 October 2012
9
結構體與類
1、類是引用類型,結構是值類型。(既然class是引用類型,class可以設爲null。但是我們不能將struct設爲null,因爲它是值類型。)
2、結構不支持繼承。
3、結構不能聲明默認的構造函數。
10
enum 枚舉
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
默認情況下,第一個枚舉符號的值是 0.
Days.Mon 1
Days.Fri 5
11
類的 構造函數 是類的一個特殊的成員函數,當創建類的新對象時執行。
析構函數用於在結束程序(比如關閉文件、釋放內存等)之前釋放資源。析構函數不能繼承或重載。
static 關鍵字把類成員定義爲靜態的。當我們聲明一個類成員爲靜態時,意味着無論有多少個類的對象被創建,只會有一個該靜態成員的副本。
也可以把一個成員函數聲明爲 static。這樣的函數只能訪問靜態變量。靜態函數在對象被創建之前就已經存在。
using System;
namespace StaticVarApplication
{
class StaticVar
{
public static int num;
public void count()
{
num++;
}
public static int getNum()
{
return num;
}
}
class StaticTester
{
static void Main(string[] args)
{
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("變量 num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
變量 num: 3
12 繼承
基類 Person 和派生類 Employee 都有一個名爲 Getinfo 的方法。通過使用 base 關鍵字,可以從派生類中調用基類的 Getinfo 方法。
using System;
public class Person
{
protected string ssn = "444-55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
// Calling the base class GetInfo method:
base.GetInfo();
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
本示例顯示如何指定在創建派生類實例時調用的基類構造函數。
using System;
public class BaseClass
{
int num;
public BaseClass()
{
Console.WriteLine("in BaseClass()");
}
public BaseClass(int i)
{
num = i;
Console.WriteLine("in BaseClass(int i)");
}
public int GetNum()
{
return num;
}
}
public class DerivedClass : BaseClass
{
// This constructor will call BaseClass.BaseClass()
public DerivedClass() : base()
{
}
// This constructor will call BaseClass.BaseClass(int i)
public DerivedClass(int i) : base(i)
{
}
static void Main()
{
DerivedClass md = new DerivedClass();
DerivedClass md1 = new DerivedClass(1);
}
}
13
abstract class是抽象類.比如說,圓形和三角形都是形狀.圓形和三角形有具體的表現方式,可以說是具體類.而形狀沒辦法具體表現出來,就是抽象類.抽象類可以有自己的屬性,可以有非abstract 的方法.
請注意,下面是有關抽象類的一些規則:
您不能創建一個抽象類的實例。
您不能在一個抽象類外部聲明一個抽象方法。
通過在類定義前面放置關鍵字 sealed,可以將類聲明爲密封類。當一個類被聲明爲 sealed 時,它不能被繼承。抽象類不能被聲明爲 sealed。
當有一個定義在類中的函數需要在繼承類中實現時,可以使用虛方法。虛方法是使用關鍵字 virtual 聲明的。虛方法可以在不同的繼承類中有不同的實現。對虛方法的調用是在運行時發生的。
動態多態性是通過 抽象類 和 虛方法 實現的。
interface是接口.相當於某些具體對象的交叉點,這個點本身不能做什麼,只能把操作傳達給各個具體的對象,讓對象自己去執行.接口的數據成員必須是靜態且不能修改的(static final),所有的方法都是虛方法.