C#一些隐藏语法 返回
C#论坛
老数据
2002
悬赏:5 飞吻
1.
string hello = "world";
var o = new { hello };
Console.WriteLine(o.hello);2.
Decimal = M
Float = F
Double = D
例子:
double d = 30D;
3.
if(dictionary.ContainsKey(key))
{
value = dictionary[key];
...
}
//简化
if(dictionary.TryGetValue(key, out value))
{ ... }4.
string format = "000;-#;(0)"; string pos = 1.ToString(format); // 001 string neg = (-1).ToString(format); // -1 string zer = 0.ToString(format); // (0)
5.构造函数添加默认值
class Example{ public Example(int value1)
: this(value1, "Default Value")
{
} public Example(int value1, string value2)
{
m_Value1 = value1;
m_value2 = value2;
} int m_Value1; string m_value2;
}6.
new String('0',22);
//等于 00000000000000000000007.
Enumerable.Range(0, 15) //等于数组 {1,2,3,4...,15}8、执行不安全代码
string str = "some string";
Console.WriteLine(str);
unsafe
{
fixed (char* s = str)
{
char* c = s;
while (*c != '\0')
{
*c = Char.ToUpper(*c++);
}
}
}