文章目录
- 一、操作符详解
- 1、操作符概览
- 2、操作符的本质
- 3、操作符的优先级
- 4、同级操作符的运算顺序
- 5、 各类操作符的示例
- 二、表达式,语句详解
- 1. 表达式的定义
- 2. 各类表达式概览
- 3. 语句的定义
- 4. 语句详解
一、操作符详解
C# 中的操作符是用于执行程序代码运算的符号,它们可以对一个或多个操作数进行操作并返回结果。
1、操作符概览
类别 | 运算符 |
---|---|
基本 | x.y f(x) a[x] x++ x-- new typeof default checked unchecked delegate sizeof -> |
一元 | + - ! ~ ++x --x (T)x await &x *x |
乘法 | * / % |
加减 | + - |
移位 | >> << |
关系和类型检测 | < > <= >= is as |
相等 | == != |
逻辑“与” | & |
逻辑 XOR | ^ |
逻辑 OR | | |
条件 AND | && |
条件 OR | || |
null 合并 | ?? |
条件 | ?: |
lambda 表达式 | = *= /= %= += -= <<= >>= &= ^= |= => |
- 操作符(Operator )也译为“运算符”
- 操作符是用来操作数据的,被操作符操作的数据称为操作数( Operand )
2、操作符的本质
-
操作符的本质是函数(即算法)的“简记法”
-
假如没有发明“+”、只有Add函数,算式
3+4+5
将可以写成Add(Add(3,4),5)
-
假如没有发明“x”、只有Mul函数,那么算式
3+4x5
将只能写成Add(3,Mul(4,5)),
注意优先级
-
-
操作符不能脱离与它关联的数据类型
- 可以说操作符就是与固定数据类型相关联的一套基本算法的简记法
namespace CreateOperator {class Program{static void Main(string[] args){int x = 5;int y = 4;int z = x / y;Console.WriteLine(z);double x1 = 5;double y1 = 4;double z1 = x1 / y1;Console.WriteLine(z1);}} }
- 示例:为自定义数据类型创建操作符
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace CreateOperator {class Program{static void Main(string[] args){//List<Person> nation = Person.GetMarry(persion1,persion2);//foreach( var p in nation){// Console.WriteLine(p.Name);//}List<Person> nation = persion1 + persion2;foreach( var p in nation){Console.WriteLine(p.Name);}}}class Person{public string Name;public static List<Person> operator +(Person p1, Person p2){List<Person> people = new List<Person> { };people.Add(p1);people.Add(p2);for (int i = 0; i < 11; i++){Person child = new Person();child.Name = p1.Name + '&' + p2.Name;people.Add(child);}return people;}} }
3、操作符的优先级
- 操作符的优先级
- 可以使用
圆括号
提高被括起来表达式的优先级 - 圆括号可以
嵌套
- 不像数学里有方括号和花括号,在C#语言里“0”与"0”有专门的用途
- 可以使用
4、同级操作符的运算顺序
除了带有赋值
功能的操作符,同优先级
操作符都是由左向右
进行运算- 带
有赋值
功能的操作符的运算顺序是由右向左
- 与数学运算不同,计算机语言的
同优先级
运算没有“结合率”- 3+4+5只能理解为Add(Add(3,4),5)不能理解为Add(3,Add(4,5))
- 带
5、 各类操作符的示例
. 操作符
namespace OperatorExample
{class Program{static void Main(string[] args){//. 操作符Form a = new Form();a.Text = "my form";a.ShowDialog();}}
}
F(x) 函数调用操作符
namespace OperatorExample
{class Program{static void Main(string[] args){//F(x) 函数调用操作符calure cc = new calure();int dd = cc.Add(1, 2);Console.WriteLine(dd);//委托Action ee = new Action(cc.print);ee();}}class calure{public int Add(int a, int b){return a + b;}public void print(){Console.WriteLine("Hello");}}
}
a[x]
namespace OperatorExample
{class Program{static void Main(string[] args){int[] intarray = new int[10];console.writeline(intarray[1]);int[] intarray2 = new int[5]{11,22,33,44,55};console.writeline(intarray2[3]);dictionary<string,student> stdob = new dictionary<string,student>();for (int i = 0; i < 101; i++){student stu = new student();stu.name = "学生" + i.tostring();stu.score = 100 + i;stdob.add(stu.name,stu);}console.writeline(stdob["学生12"].name +"; "+ stdob["学生11"].score);}}class student{public string Name;public int Score;}}
x++ x–
namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;x++;Console.WriteLine(x);x--;Console.WriteLine(x);int y = x++;Console.WriteLine(x);Console.WriteLine(y);}}
}
typeof
namespace OperatorExample
{class Program{static void Main(string[] args){Type x = typeof(int);Console.WriteLine(x.Name);Console.WriteLine(x.FullName);Console.WriteLine(x.Namespace);int a = x.GetMethods().Length;Console.WriteLine(a);foreach (var item in x.GetMethods()){Console.WriteLine(item.Name);}}}
}
default
namespace OperatorExample
{class Program{static void Main(string[] args){int a = default(int);Console.WriteLine(a);Form a = default(Form);Console.WriteLine(a == null);leved ll = default(leved);Console.WriteLine(ll);leved22 dd = default(leved22);Console.WriteLine(dd);}enum leved{mid,low,High}enum leved22{mid = 1,low = 2,High = 3,def = 0}}
}
new
namespace OperatorExample
{class Program{static void Main(string[] args){Form myForm = new Form();myForm.Text = "hello";myForm.ShowDialog();Form myFormOne = new Form() { Text = "my text" };myFormOne.ShowDialog();// var 自动推断类型var myTx = new { text = "1234", age = 55 };Console.WriteLine(myTx.text);Console.WriteLine(myTx.age);Console.WriteLine(myTx.GetType().Name);}}
}
checked uncheckded
namespace OperatorExample
{class Program{static void Main(string[] args){uint x = uint.MaxValue;Console.WriteLine(x);string aa = Convert.ToString(x, 2);Console.WriteLine(aa);//检测checked{try{uint y = x + 1;Console.WriteLine(y);}catch (OverflowException ex){//捕获异常Console.WriteLine("异常");}}}}
}
**delegate **
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace ddExample
{public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.myClick.Click += myClick_Click;//delegatethis.myClick.Click += delegate (object sender, RoutedEventArgs e){this.myTextBox.Text = "1111111111";};this.myClick.Click += (object sender, RoutedEventArgs e)=>{this.myTextBox.Text = "1111111111";};}void myClick_Click(object sender, RoutedEventArgs e){this.myTextBox.Text = "1111111111";}}
}
sizeof , ->
namespace OperatorExample
{class Program{static void Main(string[] args){int x = sizeof(int);Console.WriteLine(x);int x1 = sizeof(decimal);Console.WriteLine(x1);unsafe{//打印自定义数据结构的长度int y = sizeof(StudentStr);Console.WriteLine(y);//箭头操作符,操作内存StudentStr stu;stu.age = 16;stu.score = 100;StudentStr* pStu = &stu;pStu->score = 99;Console.WriteLine(stu.score);}}}struct StudentStr{public int age;public long score;}
}
+ - 正负
namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;Console.WriteLine(x);int y = -100;Console.WriteLine(y);int z = -(-x);Console.WriteLine(z);int x1 = int.MinValue;int y1 = -x1;Console.WriteLine(x1);Console.WriteLine(y1);//溢出int y2 = checked(-x1);}}
}
~ 求反
namespace OperatorExample
{class Program{static void Main(string[] args){int x = int.MinValue;int y = ~x;Console.WriteLine(x);Console.WriteLine(y);string xstr = Convert.ToString(x, 2).PadLeft(3, '0');string ystr = Convert.ToString(y, 2).PadLeft(3, '0');Console.WriteLine(xstr);Console.WriteLine(ystr);}}
}
bool
namespace OperatorExample
{class Program{static void Main(string[] args){bool x = false;bool y = x;Console.WriteLine(x);Console.WriteLine(y);StudentTwo tt = new StudentTwo(null);Console.WriteLine(tt.Name);}}class StudentTwo{public string Name;public StudentTwo(string Name){if (!string.IsNullOrEmpty(Name)){this.Name = Name;}else{throw new ArgumentException("name is empty");}}}
}
++x --x
namespace OperatorExample
{class Program{static void Main(string[] args){int x = 100;int y = ++x;Console.WriteLine(x);Console.WriteLine(y);int x1 = 100;int z = x1++;Console.WriteLine(z);}}
}
(T)x 类型转换
-
隐式(
implicit
)类型转换- 不丢失精度的转换
- 子类向父类的转换
- 装箱显式(
explicit
)类型转换
-
有可能丢失精度(甚至发生错误)的转换,即cast拆箱使用
Convert
类- ToString方法与各数据类型的Parse/TryParse方法自定义类型转换操作符示例隐式(implicit)类型转换
- 不丢失精度的转换
- 子类向父类的转换
- 装箱显式(explicit)类型转换
-
有可能丢失精度(甚至发生错误)的转换,即cast拆箱使用Convert类
- ToString方法与各数据类型的Parse/TryParse方法自定义类型转换操作符示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//隐式类型转换//不丢失精度的转换int x = int.MaxValue;long y = x;Console.WriteLine(y);// 子类向父类进行的隐式转换Teacher t = new Teacher();Human h = t;h.Eat();h.Think();Animal a = h;a.Eat();//显式类型转Console.WriteLine(ushort.MaxValue);uint x = 65536;ushort y = (ushort)x;Console.WriteLine(y);}class Animal{public void Eat(){Console.WriteLine("Eating....");}}class Human : Animal{public void Think(){Console.WriteLine("Think ...");}}class Teacher : Human{public void Teach(){Console.WriteLine("Teacher");}}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace ConertExample
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void button_1_Click(object sender, RoutedEventArgs e){double x = System.Convert.ToDouble(TextBox_1.Text);double y = System.Convert.ToDouble(TextBox_2.Text);double z = x + y;//TextBox_3.Text = System.Convert.ToString(z);TextBox_3.Text = z.ToString();double x1 = double.Parse(TextBox_1.Text);double y1 = double.Parse(TextBox_2.Text);double z1 = x1 + y1;//TextBox_3.Text = System.Convert.ToString(z);TextBox_3.Text = z1.ToString();}}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace C0onvertExplameT
{class Program{static void Main(string[] args){stone ss = new stone();ss.Age = 10;//explicitMonkey mm = (Monkey)ss;//implicitMonkey mm = ss;Console.WriteLine(mm.Age);}}class stone{public int Age;//显式类型转换操作符 explicit public static explicit operator Monkey(stone s){Monkey aa = new Monkey();aa.Age = s.Age * 100;return aa;}//隐式类型转换 implicit//public static implicit operator Monkey(stone s)//{// Monkey aa = new Monkey();// aa.Age = s.Age * 200;// return aa;//}}class Monkey{public int Age;}
}
is as
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){// is运算符Teacher t = new Teacher();var res = t is Teacher;Console.WriteLine(res);Console.WriteLine(res.GetType().FullName);var res1 = t is Human;Console.WriteLine(res1);var res2 = t is object;Console.WriteLine(res2);// as 运算符object t = new Teacher();Teacher tt = t as Teacher;if (tt != null){tt.Teach();}}class Animal{public void Eat(){Console.WriteLine("Eating....");}}class Human : Animal{public void Think(){Console.WriteLine("Think ...");}}class Teacher : Human{public void Teach(){Console.WriteLine("Teacher");}}}
}
乘
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 5;int y = 6;console.writeline(x * y);double a = 5.0;double b = 4.0;var z = a * b;console.writeline(z);console.writeline(z.gettype().name);int a = 5;double b = a;console.writeline(b.gettype().name);int x1 = 5;double y1 = 2.5;console.writeline(x1 * y1);var z = x1 * y1;console.writeline(z.gettype().name);}}
}
除
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 5;int y = 4;Console.WriteLine(x / y);double x1 = 5.0;double y1 = 4.0;Console.WriteLine(x1 / y1);double a = double.PositiveInfinity;double b = double.NegativeInfinity;Console.WriteLine(a / b);double c = (double)5 / 4;Console.WriteLine(c);double d = (double)(5 / 4);Console.WriteLine(d);}}
}
取余 %
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){for (int i = 0; i < 100; i++){Console.WriteLine(i % 10);}double x = 3.5;double y = 3;Console.WriteLine(x % y); }}
}
加法 + ,减法 -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//加法 +int x = 5;double y = 1.5;console.writeline(x + y);console.writeline((x + y).gettype().name);string a = "123";string b = "abc";console.writeline(a + b);//减法 -int x = 10;double y = 5.0;var z = x - y;Console.WriteLine(z);Console.WriteLine(z.GetType().Name); }}
}
位移 >> <<
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){//位移,左移一位即原值*2int x = 7;int y = x << 2;Console.WriteLine(y);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');Console.WriteLine(xStr);string yStr = Convert.ToString(y, 2).PadLeft(32, '0');Console.WriteLine(yStr);//右移一位即原值/2int x = 100;int y = x >> 2;Console.WriteLine(y);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');Console.WriteLine(xStr);string yStr = Convert.ToString(y, 2).PadLeft(32, '0');Console.WriteLine(yStr);//一直位移的情况下,会出现类型溢出的情况,需要checked}}
}
**比较运算符 <> == != **
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int a = 10;int b = 5;var c = a < b;Console.WriteLine(c);Console.WriteLine(c.GetType().FullName);char 类型比较大小,转换成ushort 比较大小char a = 'a';char b = 'A';var res = a > b;Console.WriteLine(res);ushort au = (ushort)a;ushort bu = (ushort)b;Console.WriteLine(au);Console.WriteLine(bu);//对齐后,挨个比较unicodestring str1 = "abc";string str2 = "Abc";Console.WriteLine(str1.ToLower() == str2.ToLower());}}
}
**逻辑“与”& ; 逻辑XOR ^ ; 逻辑 OR | **
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 7;int y = 10;// 按位求与,二进制1为真,0为假,真假为假,真真为真,假假为假int z = x & y;Console.WriteLine(z);string xStr = Convert.ToString(x,2).PadLeft(32,'0');string yStr = Convert.ToString(y,2).PadLeft(32,'0');string zStr = Convert.ToString(z,2).PadLeft(32,'0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);// 按位求或,二进制1为真,0为假,真假为真,真真为真,假假为假int z = x | y;Console.WriteLine(z);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');string yStr = Convert.ToString(y, 2).PadLeft(32, '0');string zStr = Convert.ToString(z, 2).PadLeft(32, '0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);// 按位求异或,二进制1为真,0为假,不一样为真,一样为假int z = x ^ y;Console.WriteLine(z);string xStr = Convert.ToString(x, 2).PadLeft(32, '0');string yStr = Convert.ToString(y, 2).PadLeft(32, '0');string zStr = Convert.ToString(z, 2).PadLeft(32, '0');Console.WriteLine(xStr);Console.WriteLine(yStr);Console.WriteLine(zStr);}}
}
条件与 && 条件或 ||
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 10;int y = 5;double a = 10.5;double b = 5;if(x>y && a>b){Console.WriteLine("真");}else{Console.WriteLine("假");}if (x < y || a > b){Console.WriteLine("真");}else{Console.WriteLine("假");}}}
}
null合并 ??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int? x = null;//x = 100;Console.WriteLine(x);Console.WriteLine(x.Value);int y = x ?? 1;Console.WriteLine(y);}}
}
条件 ?:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int x = 60;string str = x >= 60 ? "及格" : "不及格 ";Console.WriteLine(str);}}
}
**赋值 *= += -= /= %= <<= >>= &= ^= |= **
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConversionExample
{class Program{static void Main(string[] args){int a = 100;int b = a += 10;Console.WriteLine(b);int c = a>>=1;Console.WriteLine(c);}}
}
二、表达式,语句详解
1. 表达式的定义
-
什么是表达式
在 C# 里,表达式是由操作数和运算符组合而成的代码片段,它能计算出一个值。操作数可以是变量、常量、字面量、方法调用等;运算符则用于指明对操作数进行何种运算。 -
C#语言对表达式的定义
- 算法逻辑的最基本(最小)单元,表达一定的算法意图
- 因为操作符有优先级,所以表达式也就有了优先级
2. 各类表达式概览
-
C#语言中表达式的分类
- A value. Every value has an associated type.任何能得到值的运算(回顾操作符和结果类型)
- A variable. Every variable has an associated type.
int x = 10; int y; y = x;
- A namespace.
//A namespace System.Windows.Forms.Form myForm;
- A type.
// A typevar t = typeof(Int32);
- A method group.例如:Console.WriteLine,这是一组方法,重载决策决定具体调用哪一个
//A method groupConsole.WriteLine("Hello");
Console.WriteLine 有19个方法供使用
- A null literal.
//A null literalForm MyForm = null;
- An anonymous function.
Action a = delegate () { Console.WriteLine("hello"); }; a();
- A property access.
// a propety accessForm my = new Form();my.Text = "my Text";my.ShowDialog();
- An event access.
static void Main(string[] args) {Form myFo = new Form();myFo.Load += myFo_Load;myFo.ShowDialog(); }static void myFo_Load(object sender, EventArgs e) {Form form = sender as Form;if (form == null){return;}form.Text = "new Text"; }
- An indexer access.
List<int> listone = new List<int> { 1, 22, 333 };int xOne = listone[2];
- Nothing.对返回值为void的方法的调用
//NothingConsole.WriteLine("Hello");
- 复合表达式的求值
- 注意操作符的优先级和同优先级操作符的运算方向
3. 语句的定义
-
Wikipedia对语句的定义
语句是高级语言的语法
编译语言和机器语言只有指令(高级语言中的表达式对应低级语言中的指令),语句等价于一个或一组有明显逻辑关联的指令。
-
C#语言对语句的定义
- C#语言的语句除了能够让程序员“顺序地”(sequentially)表达算法思想,还能通过条件判断、跳转和循环等方法控制程序逻辑的走向
- 简言之就是:陈述算法思想,控制逻辑走向,
完成有意义
的动作(action) - C#语言的语句由分号(;)结尾,但由分号结尾的不一定都是语句
语句
一定是出现在方法体
里
4. 语句详解
- 声明语句
int xSF = 100;
var x1 = 200;
const int xC = 100;
- 表达式语句
Console.WriteLine("Hello World");new Form();int xT = 100;
xT = 200;xT++;
xT--;
++xT;
--xT;
- 块语句
{int XTo = 100;if (XTo < 100)Console.WriteLine("不及格");
}
- 标签语句
{hello: Console.WriteLine("Hello World");goto hello;}
- 块语句的作用域
int ykuai = 100;{int xkuai = 200;Console.WriteLine(ykuai);}// 无法在块语句外打印xkuaiConsole.WriteLine(xkuai);
- if 语句
int score = 59;if (score >= 80){Console.WriteLine("A");}else if (score >= 60){Console.WriteLine("B");}else if (score >= 40){Console.WriteLine("C");}else{Console.WriteLine("D");}
- switch 语句
static void Main(string[] args)
{Level myLevel = new Level();switch (myLevel){case Level.High:Console.WriteLine(Level.High);break;case Level.Mid:Console.WriteLine(Level.Mid);break;case Level.Low:Console.WriteLine(Level.Low);break;default:Console.WriteLine("凉凉");break;}
}enum Level
{High,Mid,Low
}
- try 语句,尽可能多的去处理异常
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace ExpressionExample
{class Program{static void Main(string[] args){Count cc = new Count();int res = cc.Add("122222222", "122222222");Console.WriteLine(res);}}class Count{public int Add(string a, string b){int x = 0;int y = 0;bool error = false;try{x = int.Parse(a);y = int.Parse(b);}catch (ArgumentNullException ex){Console.WriteLine(ex.Message);error = true;}catch (FormatException ex){Console.WriteLine(ex.Message);error = true;}catch (OverflowException ex){Console.WriteLine(ex.Message);error = true;}finally{if (error){Console.WriteLine("有错误不能计算");}else{Console.WriteLine("一切正常");}}int z = x + y;return z;}}
}
- while 循环语句
namespace ExpressionExample
{class Program{static void Main(string[] args){bool next = true;while (next){Console.WriteLine("请输入数字1:");string str1 = Console.ReadLine();int a1 = int.Parse(str1);Console.WriteLine("请输入数字2:");string str2 = Console.ReadLine();int a2 = int.Parse(str2);if (a1 + a2 == 100){next = false;Console.WriteLine("已经 100");}else{Console.WriteLine("请继续");}}}}
}
- do while 循环语句
namespace ExpressionExample
{class Program{static void Main(string[] args){bool nextDo = false;do{Console.WriteLine("请输入数字1:");string str1 = Console.ReadLine();int a1 = 0;try{a1 = int.Parse(str1);}catch (Exception ex){Console.WriteLine(ex.Message);continue;}Console.WriteLine("请输入数字2:");string str2 = Console.ReadLine();int a2 = 0;try{a2 = int.Parse(str2);}catch (Exception ex){Console.WriteLine(ex.Message);continue;}if (a1 == 100 || a2 == 100){Console.WriteLine("100 直接退出");break;}if (a1 + a2 == 100){nextDo = false;Console.WriteLine("已经 100");}else{nextDo = true;Console.WriteLine("请继续");}} while (nextDo);}}
}
- for 循环语句,99乘法表
namespace ExpressionExample
{class Program{static void Main(string[] args){for (int i = 1; i < 10; i++){for (int j = 1; j < 10; j++){if (i >= j){int z = i * j;Console.Write("{0} * {1} = {2} ", i, j, z);}else{Console.WriteLine("");break;}}}}}
}
- 迭代器 与 foreach
namespace ExpressionExample
{class Program{static void Main(string[] args){int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };IEnumerator values = ints.GetEnumerator(); //指月while (values.MoveNext()){Console.WriteLine(values.Current);}//重置values.Reset();while (values.MoveNext()){Console.WriteLine(values.Current);}// foreach 语句foreach (int i in ints){Console.WriteLine(i);}}}
}