Skip to content
Open
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,20 +1,20 @@
<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="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ public void WhenAssigningValuesToOtherType_ThenUseCastExpressionForExplicitConve
Assert.Equal(10, number);
}

[Fact]
public void WhenCastingAnOverflowingValue_ThenUncheckedWrapsAndCheckedThrows()
{
// arrange
long tooBig = long.MaxValue;

// act & assert
Assert.Equal(-1, unchecked((int)tooBig));
Assert.Throws<OverflowException>(() => checked((int)tooBig));
}

[Fact]
public void WhenAssigningBaseToDerivedType_ThenUseCastExpressionForExplicitConversion()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using TypeCheckingAndCastingInCSharp.Models;
using Xunit;

namespace TypeCheckingAndCastingInCSharp.Tests
Expand Down Expand Up @@ -94,14 +95,26 @@ public void WhenTypeTestingAtRunTime_ThenUseGeTypeAndTypeof()
[Fact]
public void WhenTypeCheckingValues_ThenIsOperatorReturnsBoolean()
{
int number = 16;
string name = "John Smith";
object number = 16;
object name = "John Smith";

Assert.True(number is int); // true
Assert.True(name is string); // true
Assert.False(number is bool); // false
}

[Fact]
public void WhenCheckingADerivedInstance_ThenIsMatchesTheBaseTypeAndGetTypeDoesNot()
{
// arrange
Animal animal = new Horse();

// act & assert
Assert.True(animal is Animal); // compatible type
Assert.False(animal.GetType() == typeof(Animal)); // not the exact type
Assert.True(animal.GetType() == typeof(Horse)); // the exact type
}

[Fact]
public void WhenCastingValues_ThenUseTypePatterns()
{
Expand Down
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
Loading