public class Dog
{
public string Name { get ; set ; }
public Guid Id { get ; } = Guid . NewGuid ( ) ;
public DateTime DateOfBirth { get ; set ; }
private int AgeInDays => DateTime . Now . Subtract ( DateOfBirth ) . Days ;
public int Age => AgeInDays / 365 ;
public int AgeInDogYears => AgeInDays * 7 / 365 ;
public override string ToString ( ) =>
$ "{ Name } ({ Age } | { AgeInDogYears } ) [ID: { Id } ]";
}
public Dictionary < string , object > Metadata { get ; } = new ( ) ;
public object this [ string key ]
{
get { return Metadata [ key ] ; }
set { Metadata [ key ] = value ; }
}
var pimpedli = new Dog
{
Name = "Pimpedli" ,
DateOfBirth = new DateTime ( 2006 , 06 , 10 ) ,
[ "Chip azonosító" ] = "123125AJ"
} ;
var dogs = new Dictionary < string , Dog >
{
[ "banan" ] = banan ,
[ "watson" ] = watson ,
[ "unnamed" ] = unnamed ,
[ "unknown" ] = unknown ,
[ "pinmpedli" ] = pimpedli
} ;
foreach ( var dog in dogs )
Console . WriteLine ( $ "{ dog . Key } - { dog . Value } ") ;
// szándékosan hibás
var dogs = new Dictionary < string , Dog >
{
[ banan . Name ] = banan ,
[ watson . Name ] = watson ,
[ unnamed . Name ] = unnamed ,
[ unknown . Name ] = unknown ,
[ pimpedli . Name ] = pimpedli
} ;
//ArgumentNullException!
var dogs = new Dictionary < string , Dog >
{
[ nameof ( banan ) ] = banan ,
[ nameof ( watson ) ] = watson ,
[ nameof ( unnamed ) ] = unnamed ,
[ nameof ( unknown ) ] = unknown ,
[ nameof ( pimpedli ) ] = pimpedli
} ;
var dogs = new Dictionary < string , Dog >
{
{ nameof ( banan ) , banan } ,
{ nameof ( watson ) , watson } ,
{ nameof ( unnamed ) , unnamed } ,
{ nameof ( unknown ) , unknown } ,
{ nameof ( pimpedli ) , pimpedli }
} ;
AgeInDays w Elvis/Kozsó operator
private int ? AgeInDays => ( - DateOfBirth ? . Subtract ( DateTime . Now ) ) ? . Days ;
public record class DogRec (
Guid Id ,
string Name ,
DateTime ? DateOfBirth = null ,
Dictionary < string , object > Metadata = null
) ;
public record DogRecExt
{
public string Name { get ; init ; }
public Guid Id { get ; } = Guid . Empty ;
public DateTime ? DateOfBirth { get ; set ; }
public Dictionary < string , object > Metadata { get ; } = new ( ) ;
private int ? AgeInDays => ( - DateOfBirth ? . Subtract ( DateTime . Now ) ) ? . Days ;
public int ? Age => AgeInDays / 365 ;
public int ? AgeInDogYears => AgeInDays * 7 / 365 ;
public object this [ string key ]
{
get { return Metadata [ key ] ; }
set { Metadata [ key ] = value ; }
}
}