Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
coolcsn committed Oct 5, 2019
1 parent 7c5e550 commit 72c8db8
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
37 changes: 37 additions & 0 deletions DotNetConf2019C8Part1.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29318.209
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotNetConf2019C8Part1", "DotNetConf2019C8Part1\DotNetConf2019C8Part1.csproj", "{87EC18E8-D16A-481C-B4C8-12D380CF0746}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonStandardLibrary", "PersonStandardLibrary\PersonStandardLibrary.csproj", "{1D722957-FCF3-4051-A026-B6BD1794F364}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonCoreLibrary", "PersonCoreLibrary\PersonCoreLibrary.csproj", "{75796B95-8CC8-4B21-85AB-26D2C2A81902}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{87EC18E8-D16A-481C-B4C8-12D380CF0746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87EC18E8-D16A-481C-B4C8-12D380CF0746}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87EC18E8-D16A-481C-B4C8-12D380CF0746}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87EC18E8-D16A-481C-B4C8-12D380CF0746}.Release|Any CPU.Build.0 = Release|Any CPU
{1D722957-FCF3-4051-A026-B6BD1794F364}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D722957-FCF3-4051-A026-B6BD1794F364}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D722957-FCF3-4051-A026-B6BD1794F364}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D722957-FCF3-4051-A026-B6BD1794F364}.Release|Any CPU.Build.0 = Release|Any CPU
{75796B95-8CC8-4B21-85AB-26D2C2A81902}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75796B95-8CC8-4B21-85AB-26D2C2A81902}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75796B95-8CC8-4B21-85AB-26D2C2A81902}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75796B95-8CC8-4B21-85AB-26D2C2A81902}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {05AD5788-BACA-4174-B36F-EA8DFC585C18}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions DotNetConf2019C8Part1/DotNetConf2019C8Part1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PersonCoreLibrary\PersonCoreLibrary.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions DotNetConf2019C8Part1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// https://www.youtube.com/watch?v=TJiLhRPgyq4
// What's new in C# 8 - Part 1
using System;
class Program
{
static void Main(string[] args)
{
var miguel = new Person("Miguel", "Angulo");
var length = GetMiddleNameLength(miguel);
Console.WriteLine(length);
}

static int GetMiddleNameLength(Person person) // Person person
{
//var middle = person.MiddleName;
//// if (middle == null) return 0; // to avoi the warning
//if (middle is null) return 0; // this works if you overwriten the equality == operator
//return middle.Length; //It is not null safe. if middle name = null you get an error

// return person.MiddleName.Length; // we get a warning it myt be null

// or
//return person.MiddleName?.Length ?? 0; // this is correct by construction you read the variable once

// if you think you know better
// use bank ! - null ignoring operator
// if (person.MiddleName is null) return 0;
// return person.MiddleName!.Length; // I know better

var middle = person.MiddleName;
if (string.IsNullOrEmpty(middle)) return 0;
return middle.Length;
}

//static int GetMiddleNameLength(Person? person)
//{
// // 1
// // { } is it an Object or is it something
// /* if ((person?.MiddleName is { })) return person.MiddleName.Length;*/ // Alt + Enter or Ctrl + .
// // 2
// //if ((person?.MiddleName is { } middle)) return middle.Length;
// // 3
// //if ((person?.MiddleName is {Length: var length })) return length;
// //return 0;
// // and now only 1 line
// return ((person?.MiddleName is { Length: var length })) ? length : 0;
//}
}
26 changes: 26 additions & 0 deletions PersonCoreLibrary/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// #nullable disable warnings
// #nullable disable annotations
// #nullable disable
public class Person
{
public string FirstName { get; set; }
public string? MiddleName { get; set; } // you have to make it nullable explicitly in C# 8.0
public string FamilyName { get; set; }

// if in the project file of this lib we have <LangVersion>8.0</LangVersion> <Nullable>enable</Nullable> we get warning middleName is not assigned
public Person(string firstName, string familyName)
{
FirstName = firstName;
// we can solve the warnig like this
MiddleName = null; // but we get the warning Cannot convert null to non-nullable reference type. Strings are not null by default anymore. You have to make string nullable
FamilyName = familyName;
}

#nullable disable
public Person(string firstName, string middleName, string familyName)
{
FirstName = firstName;
MiddleName = middleName;
FamilyName = familyName;
}
}
9 changes: 9 additions & 0 deletions PersonCoreLibrary/PersonCoreLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions PersonStandardLibrary/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Person
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string FamilyName { get; set; }


//// Severity Code Description Project File Line Suppression State
//Warning CS8618 Non-nullable property 'MiddleName' is uninitialized.Consider declaring the property as nullable.PersonStandardLibrary C:\Users\User\source\repos\DotNetConf2019C8Part1\PersonStandardLibrary\Person.cs 7 Active
public Person(string firstName, string familyName)
{
FirstName = firstName;
FamilyName = familyName;
}
public Person(string firstName, string middleName, string familyName)
{
FirstName = firstName;
MiddleName = middleName;
FamilyName = familyName;
}
}
9 changes: 9 additions & 0 deletions PersonStandardLibrary/PersonStandardLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion><!-- Without this is not giving me warning -->
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 72c8db8

Please sign in to comment.