Compare commits

..

2 Commits

Author SHA1 Message Date
syneffort 08bfc16804 generic compatible 1 year ago
syneffort 690b0d05c6 call event 1 year ago
  1. 25
      EffectiveCSharp/EffectiveCSharp.sln
  2. 10
      EffectiveCSharp/EffectiveCSharp/EffectiveCSharp.csproj
  3. 30
      EffectiveCSharp/EffectiveCSharp/LanguageComponent/CallEvent.cs
  4. 19
      EffectiveCSharp/EffectiveCSharp/Program.cs
  5. 35
      EffectiveCSharp/EffectiveCSharp/UseGeneric/GenericCompatible.cs

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34511.84
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EffectiveCSharp", "EffectiveCSharp\EffectiveCSharp.csproj", "{1B79263C-F9E0-4FAC-8550-E91173EB3A2F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1B79263C-F9E0-4FAC-8550-E91173EB3A2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1B79263C-F9E0-4FAC-8550-E91173EB3A2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1B79263C-F9E0-4FAC-8550-E91173EB3A2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1B79263C-F9E0-4FAC-8550-E91173EB3A2F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {18E619C9-0514-4336-A32F-36E7B1205A92}
EndGlobalSection
EndGlobal

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EffectiveCSharp.LanguageComponent
{
internal class CallEvent
{
public event EventHandler<string> Event;
public void Work()
{
int cnt = 0;
while (cnt < 10)
{
cnt++;
RaiseUpdate(cnt.ToString());
Thread.Sleep(1000);
}
}
private void RaiseUpdate(string contents)
{
Event?.Invoke(this, contents);
}
}
}

@ -0,0 +1,19 @@
using EffectiveCSharp.LanguageComponent;
using EffectiveCSharp.UseGeneric;
namespace EffectiveCSharp
{
internal class Program
{
static void Main(string[] args)
{
GenericCompatible.AreEqual(new { test = 1 }, new { test = 2 });
GenericCompatible.BetterAreEqual(new { test = 1 }, new { test = 2 }); // 컴파일 타임 체크 가능
//CallEvent callEvent = new CallEvent();
//callEvent.Event += (s, e) => Console.WriteLine(e);
//callEvent.Work();
}
}
}

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EffectiveCSharp.UseGeneric
{
internal class GenericCompatible
{
public static bool AreEqual<T>(T left, T right)
{
if (left == null)
return right == null;
if (left is IComparable<T>)
{
IComparable<T> lval = left as IComparable<T>;
if (right is IComparable<T>)
return lval.CompareTo(right) == 0;
else
throw new ArgumentException("Type does not implement IComparable<T>", nameof(right));
}
else
{
throw new ArgumentException("Type does not implement IComparable<T>", nameof(left));
}
}
public static bool BetterAreEqual<T>(T left, T right) where T : IComparable<T>
{
return left.CompareTo(right) == 0;
}
}
}
Loading…
Cancel
Save