do statements static void Main() {
string s;
do { s = Console.ReadLine(); }
while (s != "Exit");
}
for statements static void Main(string[] args) {
for (int i = 0; i < args.Length; i++)
Console.WriteLine(args[i]);
}
foreach statements static void Main(string[] args) {
foreach (string s in args)
Console.WriteLine(s);
}
break statements static void Main(string[] args) {
int i = 0;
while (true) {
if (i == args.Length)
break;
Console.WriteLine(args[i++]);
}
}
continue statements static void Main(string[] args) {
int i = 0;
while (true) {
Console.WriteLine(args[i++]);
if (i < args.Length)
continue;
break;
}
}
return statements static int F(int a, int b) {
return a + b;
}
static void Main() {
Console.WriteLine(F(1, 2));
return;
}
throw statements and try
statements
static int F(int a, int b) {
if (b == 0)
throw new Exception("Divide by zero");
return a / b;
}
static void Main() {
try {
Console.WriteLine(F(5, 0));
}
catch(Exception e) {
Console.WriteLine("Error");
}
}
checked and unchecked
statements
static void Main() {
int x = Int32.MaxValue;
Console.WriteLine(x + 1); // Overflow
checked {
Console.WriteLine(x + 1); // Exception
}
unchecked {
Console.WriteLine(x + 1); // Overflow
}
}
lock statements static void Main() {
A a = .;
lock(a) {
a.P = a.P + 1;
}
}
using statements static void Main() {
using (Resource r = new Resource()) {
r.F();
}
}