C#入门3.2——常量
2016-08-03 18:00
246 查看
常量是指在程序运行中不能改变的数据,定义一个常量与定义一个变量的过程类似,只是多加一个const
定义格式:const 数据类型 常量名称=常量值
说白了就是 const int a=1;
注意:一定要给常量赋值,赋值后不能改变
变量、常量、作用域
一般确定作用域有以下规则:
局部变量存在于声明该变量的块语句或方法的大括号内。
在for/while/foreach等循环语句中声明的变量,只作用于该循环体内。
总之,变量作用域为包含它的大括号。
Console.WriteLine(); //输出并换行。
Console.Write(); //仅输出,不换行,
Console.Write("\n"); //通过使用转义字符\n,输出并换行
Console.Write(@"\n"); //这里@的作用就是忽略转义字符\n,屏幕上输出了\n,而不是换行
2.让字符串跨行,有时候一个字符串写在一行中会很长(比如SQL语句),不使用@符号,一种写法是这样的:
string 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";
3.加上@之后可以使关键字作为标识符,如@int
定义格式:const 数据类型 常量名称=常量值
说白了就是 const int a=1;
注意:一定要给常量赋值,赋值后不能改变
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { const int banJing = 5; const double pi = 3.14; Console.WriteLine("圆的周长是"+2*pi*banJing); Console.WriteLine("圆的半径是"+pi*banJing*banJing); Console.ReadKey(); } } }
变量、常量、作用域
一般确定作用域有以下规则:
局部变量存在于声明该变量的块语句或方法的大括号内。
在for/while/foreach等循环语句中声明的变量,只作用于该循环体内。
总之,变量作用域为包含它的大括号。
Console.WriteLine(); //输出并换行。
Console.Write(); //仅输出,不换行,
Console.Write("\n"); //通过使用转义字符\n,输出并换行
Console.Write(@"\n"); //这里@的作用就是忽略转义字符\n,屏幕上输出了\n,而不是换行
C#中@符号的作用
1.用 @ 符号加在字符串前面表示其中的转义字符“不”被处理,比如Console.Write(@"\n"); 这里的\n就失去了换行的功能,而作为输出语句被输出。2.让字符串跨行,有时候一个字符串写在一行中会很长(比如SQL语句),不使用@符号,一种写法是这样的:
string 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";
3.加上@之后可以使关键字作为标识符,如@int
相关文章推荐
- SpringSecurity 3.2入门(5)自定义登录页面
- GreenDao3.2入门学习
- Kotlin入门篇(一),基本数据类型介绍、变量和常量的声明
- [Java入门笔记] Java语言基础(二):常量、变量与数据类型
- 算法竞赛入门经典3.2 分子量UVa1586
- spring、spring mvc 3.2 最全入门进阶Demo
- 深入浅出java入门 之 变量与常量(3)
- Android基础入门教程——8.3.14 Paint几个枚举-常量值以及ShadowLayer阴影效果
- 枚举类型入门用 Java 5.0 以类型安全的方式表示常量[摘]
- 枚举类型入门 用 Java 5.0 以类型安全的方式表示常量
- Struts2 入门(三) 之 配置文件的常量设置
- php入门三之常量
- php入门(基本,变量,常量,结构语句)
- Java入门 之 变量和常量
- Asp.net MVC3.0 入门指南 3.2视图 View
- JAVA入门笔记1:基本语法、常量、变量
- ThinkPHP3.2 常量参考
- SpringSecurity 3.2入门(7)自定义权限控制介绍
- C++ 入门教程(七) auto、decltype、常量、类型后缀 和 const
- 3.2跟我学Gradle-快速入门,Gradle的脚本结构