Feb 27, 2009

c#

摘自 c# station
c# 3.0 in a Nutshell

1. Types and Controls

value type and reference type, 见 c# 3.0 in a Nutshell, section 2.3

对于内建类型:
Value types
  Numeric
   o Signed integer (sbyte, short, int, long)
   o Unsigned integer (byte, ushort, uint, ulong)
   o Real number (float, double, decimal)
  Logical (bool)
  Character (char)
Reference types
  String (string)
  Object (object)
对于用户自定义类型,struct 和 enum 属于 value type, 其他都属于 reference.
Value types comprise most built-in types (specifically, all numeric types, the char type, and the bool type) as well as custom struct and enum types.
Reference types comprise all class, array, delegate, and interface types.
变量和参数,见 c# 3.0 in a Nutshell, section 2.8

注意,严格区分值类型和引用类型,自定义 class 是引用类型,所以 pass by value 是复制该引用(其所起到的作用相当于
C++ 中 pass by reference,因为 C++ 中对象是值类型)。而 c# 中 pass by reference 则是传递该类型的引用(亦即地址),对于一个引用类型,用C++的语境来描述,则是传递引用的引用。
注意,使用应用传递 ref,要在声明和调用时,同时显示说明。
Integral Types

Type Size (in bits) Range
sbyte 8 -128 to 127, signed byte
byte 8 0 to 255
short 16 -32768 to 32767
ushort 16 0 to 65535
int 32 -2147483648 to 2147483647
uint 32 0 to 4294967295
long 64 -9223372036854775808 to 9223372036854775807
ulong 64 0 to 18446744073709551615
char 16 0 to 65535, a single Unicode char

Floating Point and Decimal Types

Type Size (in bits) precision Range
float 32 7 digits 1.5 x 10-45 to 3.4 x 1038
double 64 15-16 digits 5.0 x 10-324 to 1.7 x 10308
decimal 128 28-29 decimal places 1.0 x 10-28 to 7.9 x 1028

The string Type
string s = "Hello World!";
Escape Sequence Meaning
\' Single Quote
\" Double Quote
\\ Backslash
\0 Null, not the same as the C# null value
\a Bell
\b Backspace
\f form Feed
\n Newline
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab

C# Operators

Category (by precedence) Operator(s) Associativity
Primary x.y f(x) a[x] x++ x-- new typeof default checked unchecked delegate left
Unary + - ! ~ ++x --x (T)x left
Multiplicative * / % left
Additive + - left
Shift << >> left
Relational < > <= >= is as left
Equality == != right
Logical AND & left
Logical XOR ^ left
Logical OR | left
Conditional AND && left
Conditional OR || left
Null Coalescing ?? left
Ternary ?: right
Assignment = *= /= %= += -= <<= >>= &= ^= |= => right
The checked operator tells the runtime to generate an OverflowException rather than failing silently when an integral expression or statement exceeds the arithmetic limits of that type. The checked operator affects expressions with the ++, --, (unary) -, +, -, *, /, and explicit conversion operators between integral types.
Regardless of the checked compiler switch, (constant? 只检查常数表达式) expressions evaluated at compile time are always overflow-checked—unless you apply the unchecked operator.
A delegate variable is assigned a method dynamically. This is useful for writing plug-in methods. In this example, we have a utility method named Transform that applies a transform to each element in an integer array. The Transform method has a delegate parameter, for specifying a plug-in transform.

Array Types (Array 是一个类

int[] myInts = { 5, 10, 15 };
bool[][] myBools = new bool[2][]; // jagged arrays, memory efficient
myBools[0] =
new bool[2];
myBools[1] =
new bool[1];
double[,] myDoubles = new double[2, 2]; //multi-dimension array. common usage.
string[] myStrings = new string[3];

注意区分 jagged arrays, myBools[][], and multi-dimension arrays, myDoubles[,], is that a multi-dimension array will allocate memory for every element of each dimension, whereas a jagged array will only allocate memory for the size of each array in each dimension that you define. Most of the time, you'll be using multi-dimension arrays, if you need multiple dimensions, and will only use jagged arrays in very special circumstances when you are able to save significant memory by explicitly specifying the sizes of the arrays in each dimension.

Control statements
除了常用的 if, else, else if, switch, for, do, while, break, continue 外,有 foreach

string[] names = {"Cheryl", "Joe", "Matt", "Robert"};

foreach
(string person in names)
{
Console.WriteLine("{0} ", person);
}

2. 最简单的程序
using System;

class Booleans
{
public static void Main()
{
bool content = true;
bool noContent = false;

string s = "Hello World!";

Console.WriteLine("It is {0} that C# Station provides C# programming language content.", content);
Console.WriteLine("The statement above is not {0}.", noContent);
Console.WriteLine(s);
}
}

注意里面的main() 是 static,故在main 中要调用非static函数,必须先生成 object.

using System;

class simple
{
public static void Main()
{
string s = "Hello World!";
Console.WriteLine(s);

simple os = new simple();
os.f(s);
}

int f(string s)
{
Console.WriteLine("In Function f");
Console.WriteLine(s);

this.g(s); g(s); //both are OK

return 0;
}

int g(string s)
{
Console.WriteLine("In Function g");
Console.WriteLine(s);
return 0;
}
}



函数的参数有4中,value, ref, out, and params.
默认为 value

类似于 C/C++ 中的形参,value 类型,The actual value of the argument is copied on the stack. Variables given by value parameters are local and any changes to that local variable do not affect the value of the variable used in the caller's argument.

传递引用的参数,亦即输入/输出参数,ref 类型, This means that a reference to the parameter is copied to the method. This reference still refers to the same object on the heap as the original reference used in the caller's argument. This means any changes to the local reference's object also changes the caller reference's object. The code can't change the reference, but it can make changes to the object being referenced. You can think of this as a way to have an input/output parameter.

用于输出的参数,out parameters are only passed back to the calling function. Because of definite assignment rules, you cannot use this variable until it has a valid value assigned.

可变数目的参数,params parameter, which lets you define a method that can accept a variable number of arguments. The params parameter must be a single dimension or jagged array.

3. Class

Access Modifier Description (who can access)
private Only members within the same type. (default for type members,即public 必须明确写明)
protected Only derived types or members of the same type.
internal Only code within the same assembly. Can also be code external to object as long as it is in the same assembly. (default for types)
protected internal Either code from derived type or code in the same assembly. Combination of protected OR internal.
public Any code. No inheritance, external type, or external assembly restrictions.
4. Generic Collections
类似于 C++ 中的STL

struct 为 value type。value type 和 reference type
在复制的时候行为不同。以下验证 struct 为 value type
StructPoint s1 = new StructPoint();//StructPoint is a struct
            s1.i = 3;
            StructPoint s2 = s1;
            s2.i = 5;
            Console.WriteLine(s1.i);
           
            Program p = new Program(); //Program is a class
            p.i = 3;
            Program p6 = p;
            p6.i = 5;
            Console.WriteLine(p.i);

Output:
3
5

0 comments: