PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL
This commit is contained in:
48
SomeOfCSharpFeatures/PropertiesIndexersRefOut/Program.cs
Normal file
48
SomeOfCSharpFeatures/PropertiesIndexersRefOut/Program.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using PropertiesIndexersRefOut;
|
||||
|
||||
Triple t1 = new Triple
|
||||
{
|
||||
First = 456,
|
||||
Second = 789
|
||||
};
|
||||
|
||||
Triple t2 = new Triple
|
||||
{
|
||||
First = 1000,
|
||||
Second = 2000
|
||||
};
|
||||
|
||||
Console.WriteLine("t1 + t2 = " + (t1 + t2));
|
||||
|
||||
CreateRandomTriple(t1, ref t2, out Triple t3);
|
||||
|
||||
Console.WriteLine("t1 = " + t1);
|
||||
Console.WriteLine("t2 = " + t2);
|
||||
Console.WriteLine("t3 = " + t3);
|
||||
|
||||
Console.WriteLine(t1["A", 1]);
|
||||
Console.WriteLine(t1["B", 2]);
|
||||
Console.WriteLine(t1["B", 3]);
|
||||
|
||||
PrintTriples(t1, t2, t3);
|
||||
|
||||
void PrintTriples(params Triple[] triples)
|
||||
{
|
||||
foreach (var t in triples)
|
||||
{
|
||||
Console.WriteLine(t);
|
||||
}
|
||||
}
|
||||
|
||||
void CreateRandomTriple(Triple t1, ref Triple t2, out Triple t3)
|
||||
{
|
||||
Random r = new Random();
|
||||
t3 = new Triple
|
||||
{
|
||||
First = r.Next(t1.First, t2.Second),
|
||||
Second = r.Next(maxValue: t2.Second, minValue: t1.First)
|
||||
};
|
||||
|
||||
t1 = t3;
|
||||
t2 = t3;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
73
SomeOfCSharpFeatures/PropertiesIndexersRefOut/Triple.cs
Normal file
73
SomeOfCSharpFeatures/PropertiesIndexersRefOut/Triple.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
namespace PropertiesIndexersRefOut;
|
||||
|
||||
public class Triple
|
||||
{
|
||||
private int first;
|
||||
public int First
|
||||
{
|
||||
get
|
||||
{
|
||||
return first;
|
||||
}
|
||||
set
|
||||
{
|
||||
first = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Second { get; set; }
|
||||
|
||||
public int Third => First + Second;
|
||||
public override string ToString()
|
||||
{
|
||||
return $"({First}, {Second}, {Third})";
|
||||
}
|
||||
|
||||
#region Indexer
|
||||
public int this[string s, int position]
|
||||
{
|
||||
get
|
||||
{
|
||||
int num;
|
||||
switch (s)
|
||||
{
|
||||
case "A":
|
||||
num = this.First;
|
||||
break;
|
||||
case "B":
|
||||
num = this.Second;
|
||||
break;
|
||||
case "C":
|
||||
num = this.Third;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Invalid position in triple (should be A, B, or C)");
|
||||
}
|
||||
int digit = 0;
|
||||
while(num > 0 && position > 0)
|
||||
{
|
||||
digit = num % 10;
|
||||
num /= 10;
|
||||
position--;
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Operator overloading
|
||||
public static Triple operator +(Triple x, Triple y)
|
||||
{
|
||||
return new Triple
|
||||
{
|
||||
First = x.First + y.First,
|
||||
Second = x.Second + y.Second
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user