当前位置:网站首页>C# 语言的基本语法结构
C# 语言的基本语法结构
2022-07-05 18:41:00 【青少年编程竞赛交流】
C# 语言的基本语法结构
知识结构:
1、数据类型
第一种分类:
- 简单数据类型:
byte
、short
、int
、long
、float
、double
、char
、bool
- 组合数据类型:
struct
、enum
、class
、interface
类型 | 描述 |
---|---|
byte | 无符号8位整型 |
(ushort) short | (无)有符号16位整型 |
(uint) int | (无)有符号32位整型 |
(ulong) long | (无)有符号64位整型 |
float | 32位浮点型 |
double | 64位浮点型 |
char | 16位unicode字符(国际标准字符集) |
bool | 布尔型 |
第二种分类:
- 值类型:作为参数传递时,传递拷贝。
- 包括:简单数据类型、struct类型、enum类型
- 引用类型:作为参数传递时,传递地址。
- 包括:class类型、数组
例1:
public struct Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void ChangeBook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "John";
}
static void PrintBook(Book bk)
{
Console.WriteLine("Book Infor:\n Price={0},Title={1},Author={2}",
bk.Price, bk.Title, bk.Author);
}
static void Main(string[] args)
{
Book bk;// = new Book();
bk.Price = 10.01;
bk.Title = "MatLab";
bk.Author = "Tom";
PrintBook(bk);
ChangeBook(bk);
PrintBook(bk);
//Book Infor:
//Price=10.01,Tile=MatLab,Author=Tom
//Book Infor:
//Price=10.01,Tile=MatLab,Author=Tom
}
}
该例子说明“值类型”传递拷贝,不改变本身所存储的值。
例2:
public class Book
{
public double Price;
public string Title;
public string Author;
}
class Program
{
static void ChangeBook(Book bk)
{
bk.Price = 1.01;
bk.Title = "Spss";
bk.Author = "John";
}
static void PrintBook(Book bk)
{
Console.WriteLine("Book Infor:\n Price={0}, Tile={1}, Author={2}",
bk.Price, bk.Title, bk.Author);
}
static void Main(string[] args)
{
Book bk = new Book(); //错误 Book bk;
bk.Price = 10.01;
bk.Title = "MatLab";
bk.Author = "Tom";
PrintBook(bk);
// Book Infor:
// Price = 10.01, Tile = MatLab, Author = Tom
ChangeBook(bk);
PrintBook(bk);
// Book Infor:
// Price = 1.01, Tile = Spss, Author = John
}
}
例3:
class Program
{
static void ChangeArrayItem(int[] array)
{
for (int i = array.Length - 1; i >= 0; i--)
{
array[i] = array.Length - 1 - i;
}
}
static void PrintArrayItem(int[] arry)
{
for (int i = 0; i < arry.Length; i++)
{
Console.Write("{0} ", arry[i]);
}
Console.WriteLine();
}
static void Main(string[] args)
{
int[] arr = new int[3];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i;
}
PrintArrayItem(arr); // 0 1 2
ChangeArrayItem(arr);
PrintArrayItem(arr); // 2 1 0
}
}
例2,例3说明“引用类型”传递地址,要改变本身所存储的值。具体应用时,要注意“值类型”和“引用类型”的区别。
2、变量与常量
- 变量定义:
变量类型 变量名;
- 常量定义:
readonly
在声明或构造函数中初始化const
在声明时初始化
例4:
public class SimpleClass
{
public int X;
public readonly int Y = 2;
public readonly int Z;
public const double Pi = 3.1415926;
public const string Etc = "...";
public SimpleClass()
{
Z = 3;
}
public SimpleClass(int p1, int p2, int p3)
{
X = p1;
Y = p2;
Z = p3;
}
}
class Program
{
static void Main(string[] args)
{
SimpleClass sp1 = new SimpleClass();
sp1.X = 1;
Console.WriteLine("sp1:x={0}, y={1}, z={2}", sp1.X, sp1.Y, sp1.Z);
// sp1: x = 1, y = 2, z = 3
SimpleClass sp2 = new SimpleClass(-1, -2, -3);
Console.WriteLine("sp2:x={0} ,y={1}, z={2}", sp2.X, sp2.Y, sp2.Z);
// sp2: x = -1 ,y = -2, z = -3
Console.WriteLine("PI={0}{1}", SimpleClass.Pi, SimpleClass.Etc);
// PI = 3.1415926...
}
}
以上例子注意readonly
与const
定义常量以及使用该常量时的区别。
3、运算符与表达式
运算符:
- 一元运算符
x++
,y++
- 二元运算符
x+y
,x-y
- 三元运算符
max = (x>y)?x:y;
运算符:
- 算术运算符
+、-、*、/、%
- 关系运算符
>、>=、==、!=、<=、<
- 逻辑运算符
!、&&、||
表达式:由运算符和变量或常量组成的式子。
4、基本语句
4.1 赋值语句
变量名 = 表达式;
4.2 条件语句
第一种:
if(条件表达式)
{
语句序列;
}
第二种:
if(条件表达式)
{
语句序列;
}
else
{
语句序列;
}
第三种:
if(条件表达式1)
{
语句序列1;
}
else if(条件表达式2)
{
语句序列2;
}
else if(条件表达式N)
{
语句序列N;
}
else
{
语句序列N+1;
}
4.3 开关语句
swith(表达式)
{
case 值1:语句序列1; break;
case 值2:语句序列2; break;
case 值N:语句序列N; break;
default:语句序列N+1; break;
}
例5:
class Program
{
static void Main(string[] args)
{
Random rdm = new Random();
int i = rdm.Next(1, 5);
Console.WriteLine(i); // 1
switch (i)
{
case 1:
Console.WriteLine("Case 1.");
break;
case 2:
Console.WriteLine("Case 2.");
break;
case 3:
Console.WriteLine("Case 3.");
break;
default:
Console.WriteLine("Default Case.");
break;
}
// Case 1.
i = rdm.Next(1, 5);// 1
Console.WriteLine(i);
switch (i)
{
case 1:
case 2:
case 3:
Console.WriteLine("It's 1,2 or 3.");
break;
default:
Console.WriteLine("Not Sure What it is.");
break;
}
//It's 1,2 or 3.
}
}
以上例子注意switch
语句的语法结构,特别是每个case
语句都需要匹配break
语句。
4.4 循环语句
第一种:
for(初始化循环计数器表达式;判断循环终止条件;递增或递减循环计数器表达式)
{
语句序列;
}
第二种:
while(条件表达式)
{
语句序列;
}
第三种:
do
{
语句序列
}while(条件表达式);
第四种:
foreach(元素类型 元素 in 集合)
{
语句序列;//通常用于遍历集合中的每个元素
}
例6:
class Program
{
static void Main(string[] args)
{
int i;
int sum = 0;
for (i = 1; i <= 10; i++)
{
sum += i;
}
Console.WriteLine(sum);// 55
sum = 0;
i = 1;
while (i <= 10)
{
sum += i;
i++;
}
Console.WriteLine(sum);// 55
sum = 0;
i = 1;
do
{
sum += i;
i++;
} while (i <= 10);
Console.WriteLine(sum);// 55
}
}
例7:
class Program
{
static void Main(string[] args)
{
int[] arry = new int[] {
1, 3, 5, 7 };
foreach (int i in arry)
{
Console.WriteLine(i);
}
// 1
// 3
// 5
// 7
}
}
4.5 try…catch…finally 语句
try
{
语句序列;
}
catch(Exception ex)
{
语句序列;
}
finally
{
语句序列;
}
例8:在屏幕上输入一个整数,则屏幕上显示该整数颗“*”号。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个整数:");
// 请输入一个整数:
// abc
string sTemp = Console.ReadLine();
try
{
int iCount = int.Parse(sTemp);
for (int i = 0; i < iCount; i++)
{
Console.Write("*");
}
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("错误原因为:" + ex.Message);
// 错误原因为:输入字符串的格式不正确。
}
finally
{
Console.WriteLine("结束.");
// 结束.
}
}
}
以上例子注意try…catch…finally
语句的语法结构,该语句通常用来捕获并处理异常。
4.6 break、continue 语句
break
语句:跳出最里面的封闭循环或switch
语句中continue
语句:将控制权传递给所在封闭循环的下一次迭代
例9:
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
// 1
// 2
// 3
// 4
for (int i = 1; i <= 100; i++)
{
if (i < 99)
continue;
Console.WriteLine(i);
}
// 99
// 100
}
}
4.7 注释语句
- 单行注释:
//文字序列
- 多行注释:
/*文字序列*/
边栏推荐
- Windows Oracle 开启远程连接 Windows Server Oracle 开启远程连接
- C language makes it easy to add, delete, modify and check the linked list "suggested collection"
- [detailed explanation of AUTOSAR 14 startup process]
- Overview of video self supervised learning
- Isprs2022/ cloud detection: cloud detection with boundary nets
- Linear table - abstract data type
- max31865模块RTD测温注意事项
- 如何写出好代码 - 防御式编程
- Taishan Office Technology Lecture: from the layout height of the line, talk about the height overflow and height shrinkage of the drawing height (launched in the whole network)
- AI金榜题名时,MLPerf榜单的份量究竟有多重?
猜你喜欢
Overview of video self supervised learning
[detailed explanation of AUTOSAR 14 startup process]
Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
小程序 修改样式 ( placeholder、checkbox的样式)
5. Data access - entityframework integration
Find in MySQL_ in_ Detailed explanation of set() function usage
国内低代码开发平台靠谱的都有哪些?
How to automatically install pythn third-party libraries
You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
Web3.0时代来了,看天翼云存储资源盘活系统如何赋能新基建(下)
随机推荐
2022年阿里Android高级面试题分享,2022阿里手淘Android面试题目
c语言简便实现链表增删改查「建议收藏」
Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换
Deep copy and shallow copy [interview question 3]
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
5. 数据访问 - EntityFramework集成
Go deep into the underlying C source code and explain the core design principles of redis
紧固件行业供应商绩效考核繁琐?选对工具才能轻松逆袭!
EMQX 5.0 正式发布:单集群支持 1 亿 MQTT 连接
7-1 链表也简单fina
RPC protocol details
ICML2022 | 长尾识别中分布外检测的部分和非对称对比学习
进程间通信(IPC):共享内存
使用文件和目录属性和属性
小程序 修改样式 ( placeholder、checkbox的样式)
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
The era of Web3.0 is coming. See how Tianyi cloud storage resources revitalize the system to enable new infrastructure (Part 2)
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
AI open2022 | overview of recommendation systems based on heterogeneous information networks: concepts, methods, applications and resources