Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,95 +1,104 @@
namespace SwitchCaseExpression
namespace SwitchCaseExpression;

public static class Program
{
public static class Program
{
public static bool In<T>(this T val, params T[] vals) => vals.Contains(val);
private static readonly List<int> pleasantTemperatures = [20, 22, 24];

static void Main(string[] args)
{
SubMultipleCaseResults(22);
SubMultipleCaseResultsWithWhen(100);
SubMultipleCaseWithExtension(22);
SubMultipleCaseWithListValues(22);
SubMultipleCaseWithNewVersion(22);
}
public static bool In<T>(this T val, params T[] vals) => vals.Contains(val);

public static void SubMultipleCaseResults(int switchTemp)
{
var resultstring = string.Empty;
static void Main()
{
Console.WriteLine(SubMultipleCaseResults(22));
Console.WriteLine(SubMultipleCaseWithRangePattern(100));
Console.WriteLine(SubMultipleCaseWithGuard(100));
Console.WriteLine(SubMultipleCaseWithExtension(22));
Console.WriteLine(SubMultipleCaseWithListValues(22));
Console.WriteLine(SubMultipleCaseWithOrPattern(22));
}

switch (switchTemp)
{
case 20:
case 22:
case 24:
resultstring = "It is a pleasant day";
break;
case 30:
resultstring = "It is very hot today";
break;
default:
resultstring = "No weather report today";
break;
}
public static string SubMultipleCaseResults(int switchTemp)
{
var result = string.Empty;

Console.WriteLine(resultstring);
switch (switchTemp)
{
case 20:
case 22:
case 24:
result = "It is a pleasant day";
break;
case 30:
result = "It is hot today";
break;
case 35:
result = "It is very hot today";
break;
default:
result = "No weather report";
break;
}

public static void SubMultipleCaseResultsWithWhen(int value)
return result;
}

public static string SubMultipleCaseWithRangePattern(int value)
{
switch (value)
{
switch (value)
{
case int n when (n >= 50 && n <= 150):
Console.WriteLine("The value is between 50 and 150");
break;
case int n when (n >= 150 && n <= 200):
Console.WriteLine("The value is between 150 and 200");
break;
default:
Console.WriteLine("The number is not within the given range.");
break;
}
case >= 50 and <= 150:
return "The value is between 50 and 150";
case > 150 and <= 200:
return "The value is between 150 and 200";
default:
return "The number is not within the given range.";
}
public static void SubMultipleCaseWithExtension(int tempValue)
{
var result = tempValue switch
{
var x when x.In(20, 22, 24) => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
_ => "No weather report.",
};
}

Console.WriteLine($"{result} - with extension method");
public static string SubMultipleCaseWithGuard(int value)
{
switch (value)
{
case >= 50 and <= 150 when value % 2 == 0:
return "An even value between 50 and 150";
case >= 50 and <= 150:
return "An odd value between 50 and 150";
default:
return "The number is not within the given range.";
}
}

public static void SubMultipleCaseWithListValues(int tempValue)
public static string SubMultipleCaseWithExtension(int tempValue)
{
var result = tempValue switch
{
var templist = new List<int> { 20, 22, 24 };

var newresult = tempValue switch
{
var x when templist.Contains(x) => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
_ => "No weather report",
};
var x when x.In(20, 22, 24) => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
_ => "No weather report.",
};

Console.WriteLine($"{newresult} - result when using a list");
}
return result;
}

public static void SubMultipleCaseWithNewVersion(int tempValue)
public static string SubMultipleCaseWithListValues(int tempValue)
{
var newResult = tempValue switch
{
var resultText = tempValue switch
{
20 or 22 or 24 => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
> 35 => "Heat wave condition",
_ => "No weather report.",
};
var x when pleasantTemperatures.Contains(x) => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
_ => "No weather report",
};

Console.WriteLine($"{resultText} - result is for C# 9.0 syntax");
}
return newResult;
}

public static string SubMultipleCaseWithOrPattern(int tempValue) => tempValue switch
{
20 or 22 or 24 => "It is a pleasant day",
30 => "It is hot today",
35 => "It is very hot today",
> 35 => "Heat wave condition",
_ => "No weather report."
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions csharp-basic-topics/SwitchCaseExpressions/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading