switch
is only used with values types
switch (expression)
{
case value1:
statement 1;
break;
case value2:
statement 2;
statement 3;
break;
default:
statement 4;
break;
}
try,catch,finally
try
{
Console.WriteLine("nice started");
int l = 12;
throw new Exception("there are problem");
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
finally // called at the end and don't care if is their exception or not
{
Console.WriteLine("nice finished");
}
foreach
string[] languages = { "Java", "C#", "Python", "C++", "JavaScript" };
foreach (string lang in languages)
{
Console.WriteLine(lang);
}
Span<int> arr = stackalloc int[]{ 1, 1, 2, 3, 5, 8 };
foreach(ref int n in arr)
{
n *= 2;
}
foreach(ref readonly var n in arr)
{
Console.WriteLine(n);
}
goto
for (int i = 0; i <= 10; i++)
{
Console.WriteLine(i);
if (i == 5)
{
goto printmessage;
}
}
printmessage:
Console.WriteLine("The goto statement is executed");
yield return and yield break
IEnumerable<int> GetNumbers()
{
for (int i = 1; i <= 100; ++i)
{
Thread.Sleep(1000);
Console.WriteLine($"Produced: {i}");
yield return i;
}
}
foreach(var i in GetNumbers().Take(5))
{
Console.WriteLine($"Consumed: {i}");
}
disposing with using block
using System;
class DisposableDemoClass : IDisposable
{
public void Dispose()
{
Console.WriteLine("Dispose called!");
}
}
class Hello
{
static void Main()
{
using(var disp = new DisposableDemoClass())
{
}
}
}