这篇日志记录一下C#的常量,ASCII编码、@控制符、“+”连接符、Console.WriteLine及转译字符。
1:声明常量
常量一旦被定义,不易被修改。
(1)const声明:定义常量必须赋值
const double pai = 3.1415926; const声明的变量本身就是一个静态变量。不需要再使用static修饰。
(2):readonly声明:初始定义可不赋值,赋值之后不能修改。
Readonly int aa = 52; Readonly 不是一个静态变量。
2:ASCII编码
把一个字符类型变量转换成整数型
十进制:
int aaa = (int)'B'; Console.WriteLine(aaa); // 66
char bbb = (char)66; //B Console.WriteLine(bbb);
3:转义字符
以上就是C#中定义的转义字符。
实例:
Console.WriteLine("what is your name?\nMy name is GC"); // 换行 Console.WriteLine("C:\\ASD\\az"); // 表示电脑路径 Console.WriteLine("\"你好\"");//显示双引号
输出结果:
what is your name? My name is GC C:\ASD\az "你好"
4:@符的使用
(1):忽略转义字符
Console.WriteLine(@"what is your name?\nMy name is GC"); // 输出转义字符,不换行。
输出结果:
what is your name?\nMy name is GC
(2):让字符串跨行。
这个是这样,做php的时候写变量可以这样写:
$adminList = Db::query("SELECT p1.*,p2.title as rolename from admin p1 LEFT JOIN auth_group p2 ON p2.id = p1.gid where $map order by p1.id asc limit $num,$pagesize");
C#这样写是不可以的。想跨行,得这样写:
string strSQL = "SELECT * FROM HumanResources.Employee AS e" + " INNER JOIN Person.Contact AS c" + " ON e.ContactID = c.ContactID" + " ORDER BY c.LastName"; Console.WriteLine(strSQL);
输出结果:
SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID = c.ContactID ORDER BY c.LastName
使用@符的话:就是这样
string strSQL = @"SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID = c.ContactID ORDER BY c.LastName";
输出结果:
SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID = c.ContactID ORDER BY c.LastName
(3):在标识符中的用法
这个先搁置, 第一,我觉得这个没啥用, 第二,目前来看跟我好像没啥关系。碰到了再说。
5:+的使用
对于数字类型的变量,他是加号,用来搞运算的。
对于字符串类型的变量,他就是连接符,这个跟javascript类似。
string qa = "xiao"; string qs = " han"; string qd = qa + qs; Console.WriteLine(qd);
输出结果:
xiaohan
6:Console.WriteLine 变量格式化输出
占位符输出:
Console.WriteLine("{0} + {1} = {2}",a,b,a+b); //占位符输出
输出结果:
50 + 201 = 251
还可以控制变量输出位数:输出宽度不足,左侧用空格补齐。
Console.WriteLine("{0,6}\n + {1,4}\n --------\n {2,5}\n",a,b,a+b); //控制每行输出宽度,输出宽度不足,左侧用空格补齐
输出结果:
50 + 201 -------- 251
输出小数:
基本格式:Console.WriteLine("{0:#.00}",变量);
其中**#代表变量整数位,.00代表小数位,有几个0就表示有几个小数位。**
double a = 3.1415926; Console.WriteLine("{0:#.00}",a); Console.WriteLine("{0:#.0000}", a);
输出结果:
3.14 3.1416
用法跟上边一样:
Console.WriteLine("{0,6:C2}\n + {1,4:C2}\n --------\n {2,5:C2}\n",a,b,a+b);
输出结果:
¥3.14 + ¥1.26 -------- ¥4.40
下方是我测试使用的所有代码:控制台应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace gc { class Program { /* C#主要的运行函数,就是main函数 */ static void Main(string[] args) { int aaa = (int)'B'; //Console.WriteLine(aaa); // 66 char bbb = (char)66; //B //Console.WriteLine(bbb); //Console.WriteLine("what is your name?\nMy name is GC"); // 换行 //Console.WriteLine("C:\\ASD\\az"); // 表示电脑路径 //Console.WriteLine("\"你好\"");//显示双引号 //Console.WriteLine(@"what is your name?\nMy name is GC"); // 输出转义字符,不换行。 string strSQL = "SELECT * FROM HumanResources.Employee AS e" + " INNER JOIN Person.Contact AS c" + " ON e.ContactID = c.ContactID" + " ORDER BY c.LastName";//*/ string strS = @"SELECT * FROM HumanResources.Employee AS e INNER JOIN Person.Contact AS c ON e.ContactID = c.ContactID ORDER BY c.LastName";//*/ string qa = "guan"; string qs = " chao"; string qd = qa + qs; /*int a = 50; int b = 201; int total;//*/ double a = 3.1415926; Console.WriteLine("{0:#.00}",a); Console.WriteLine("{0:#.0000}", a); //double b = 1.258; //Console.WriteLine("{0,6:C2}\n + {1,4:C2}\n --------\n {2,5:C2}\n",a,b,a+b); //Console.WriteLine(qd); } } }