c#基础整理
1. Array
//Single-dimensional ,单维数组
string [,] names=new string[3] {"Matt", "Joanne", "Robert"};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
string[] names = {"Matt", "Joanne", "Robert"};
// Mutidimensional, 多维数组
// 不能单独访问一行,只能一个cell 一个cell进行访问
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
int[, , ] numbers2 = { {1, 2 ,3}, {1, 2 ,3}, {1, 2 ,3}};
//jagged array, 交叉数组
// 可以单独访问一行,因为一行就是一个数组
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
//int[,] numbers = new int[3, 2] { {9, 99}, {3, 33}, {5, 55}};
foreach(int i in numbers)
{
Console.Write("{0} ", i);
//out: 9 99 3 33 5 55
// array的foreach有特殊处理
// 数组的 foreach进行了特殊的处理
}
2. Pointer
int*[] p — p is a single-dimensional array of pointers to integers.
int* , 指向int类型的指针类型
。
Pointer types do not inherit from object
and no conversions exist between pointer types and object. Also, boxing and unboxing
do not support pointers.
指针类型没有从object继承,不能和object类型进行转换。因此,拆箱和装箱不支持指针类型。
3. dynamic
Q: dynamic vs var
A: var 只能应用于方法内部的局部变量; dynamic 可以应用于局部变量、字段和方法参数。
Q: dynamic 和泛型的区别?
==占位占位占位==
internal static class DynamicDemo {
public static void Main() {
dynamic value;
for (Int32 demo = 0; demo < 2; demo++) {
value = (demo == 0) ? (dynamic) 5 : (dynamic) "A";
value = value + value;
M(value);
}
}
private static void M(Int32 n) { Console.WriteLine("M(Int32): " + n); }
private static void M(String s) { Console.WriteLine("M(String): " + s); }
//M(Int32): 10
//M(String): AA
}
4. StackFrame是什么?
StackFrame 表示当前线程的调用桟中的一个方法调用。线程中执行的每个方法调用都会在线程的调用桟中创建并压入一个 StackFrame。
5. sizeof的作用?
sizeof(type)
返回类型实例内存空间大小。
sizeof(Int32); //4
sizeof(DateTime);// error,dateTime没有预定义大小,只有在运行时才能确定大小
// 但是这个type的大小必须是预定义好的。
6. 继承构造函数执行顺序
class Program
{
static void Main(string[] args)
{
var xx = new B();
Console.Read();
}
}
public class A
{
public int age { get; set; }
public A()
{
this.age = 10;
Console.WriteLine(this.age);
}
}
public class B:A
{
public string name { get; set; }
public B()
{
this.name = "hello";
Console.WriteLine(this.name);
}
}
// 10
// hello
// 先初始化父类, 再初始化子类
易错点
new DateTime() == defatult(DateTime)
。得到相等的两个DateTime实例。i++
自增运算符是先传递参数,再自增。容易混淆,在参数传递时不要使用。
class IncrementOperator_demo
{
public static void Main()
{
int i = 0;
show(i++);
show(++i);
show(i);
//0
//2
//2
Console.ReadKey();
}
static void show(int num)
{
Console.WriteLine(num);
}
}