using System;
namespace Wrox
{
public class Program
{
static void Main(string[] args)
{
var name = "Bugs Bunny";
var age = 25;
var isRabbit = true;
Type nameType = name.GetType();
Type ageType = age.GetType();
Type isRabbitType = isRabbit.GetType();
Type money = typeof(decimal);
Console.WriteLine("name is type " + nameType.ToString());
Console.WriteLine("age is type " + ageType.ToString());
Console.WriteLine("isRabbit is type " + isRabbitType.ToString());
Console.WriteLine( "money type is:" + money.ToString() );
Console.ReadLine();
}
}
}
using System;
class StringExample
{
private void DisplayStringEqual(string s1, string s2)
{
if( s1 == s2 )
{
Console.WriteLine("Str 1 is Equal Str2.");
}
else
{
Console.WriteLine("Str 1 is Not Equal Str2.");
}
}
public static int Main()
{
// 没有赋值,字符串是引用类型的,指向同一块内存
StringExample obj = new StringExample();
string s1 = "a string";
string s2 = s1;
Console.WriteLine("s1 is: " + s1);
Console.WriteLine("s2 is: " + s2);
obj.DisplayStringEqual(s1, s2);
// 如果赋值了那么就是一个新的字符串常量
s1 = "another string";
Console.WriteLine("s1 is now: " + s1);
Console.WriteLine("s2 is now: " + s2);
obj.DisplayStringEqual(s1, s2);
Console.ReadLine();
return 0;
}
}
if (string.IsNullOrEmpty(s)) { } if(s != null && s.Length ==0) {}性能比 s == ""快很多。
public string JoinIds(Listusers) { var ee = users.GetEnumerator(); StringBuilder sb = new StringBuilder(); if (ee.MoveNext()) { sb.Append("'"); sb.Append(ee.Current.Id); sb.Append("'"); while (ee.MoveNext()) { sb.Append(",'"); sb.Append(ee.Current.Id); sb.Append("'"); } } return sb.ToString(); }
c、C 货币格式。
d、D 十进制格式。
e、E 科学计数(指数)格式。
f、F 固定点格式。
g、G 常规格式。
n、N 数字格式。
r、R 往返格式,确保将已转换成字符串的数字转换回数字时具有与原数字相同的值。
x、X 十六进制格式。
double val = Math.PI; Console.WriteLine(val.ToString()); //displays 3.14159265358979
Console.WriteLine(val.ToString("E"));//displays 3.141593E+000
Console.WriteLine(val.ToString("F3"));//displays 3.142
Single val2=0.123F;Console.WriteLine(val2.ToString("p")); //displays 12.30 %
Console.WriteLine(val2.ToString("p1")); //displays 12.3 %
int val3=65535;
Console.WriteLine(val3.ToString("x")); //displays ffff
Console.WriteLine(val3.ToString("X")); //displays FFFF
decimal val4 = 323.42313m;
Console.WriteLine("{0,9:f2}",val4); // displays 323.42
//{0,9:f2}表示第0个参数,占9位右对齐,小数点精确两位
Console.WriteLine("{0,-9:f2}", val4); // -9表示占9位,左对齐