-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityConversionSample.cs
51 lines (46 loc) · 1.48 KB
/
EntityConversionSample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
namespace CodeSamples.Alterations
{
public class EntityOne
{
public int ValueOne { get; set; } = 1;
//implicit operators do not require explicit cast
public static implicit operator EntityTwo(EntityOne entity)
{
var entityTwo = new EntityTwo()
{
ValueTwo = entity.ValueOne
};
return entityTwo;
}
}
public class EntityTwo
{
public int ValueTwo { get; set; } = 2;
//explicit operators DOES require cast
public static explicit operator EntityOne(EntityTwo entity)
{
var entityOne = new EntityOne()
{
ValueOne = entity.ValueTwo
};
return entityOne;
}
}
public class EntityConversionSample : SampleExecute
{
public override void Execute()
{
Title("EntityConversionSampleExecute");
EntityOne entityOne = new EntityOne();
EntityTwo entityTwo = new EntityTwo();
EntityOne entityOneExplicit = (EntityOne)entityTwo;
EntityTwo entityTwoImplicit = entityOne;
//
Console.WriteLine($"Entity One: original = {entityOne.ValueOne}, explicit casting = {entityOneExplicit.ValueOne}");
Console.WriteLine($"Entity Two: original = {entityTwo.ValueTwo}, implicit casting = {entityTwoImplicit.ValueTwo}");
//
Finish();
}
}
}