35
loading...
This website collects cookies to deliver better user experience
public record Employee
{
public string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init; }
}
var theNewGuy = new Employee
{
Name = "Chris Smith",
Salary = 1000m,
Date = DateTime.Now()
};
But what if the object doesn't make sense until some properties are set? 🤔
public record Employee
{
public required string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init; }
}
namespace HelloWorld
{
class Hello
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
Is it possible to keep that functionality, but at the same time reduce excess indentation? 🤔
How is it possible? 🤔
NameSpace HelloWorld; public class Hello
{
static void Main (string [] args)
{
System.Console.WriteLine ("¡Hello World!");
}
}
namespace Company.Product;
Company.Product.Componentnamespace Component{
}
public record Employee
{
public required string Name { get; init; }
public decimal Salary { get; init; }
public DateTime Date{ get; init => field = value.Date(); }
}
private string _firstName;public string Name
{
get
{
return _tName;
}
set
{
if (value.Trim() == "")
throw new ArgumentException("No blank strings");_Name = value;
}
}
public string tName {get;
set
{
if (value.Trim() == "")
throw new ArgumentException("No blank strings"); field = value;
}
}
class Person
{
public required string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
class Person
{
public Person(string name) => Name = name;
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
class Person
{
public string Name { get; init => field = value.Trim(); }
public DateTime DateOfBirth { get; set => field = value.Date; }
var dotnetsafer = new
{
Name = "Dotnetsafer",
Email = "[email protected]"
};
var bar = dotnetsafer with {Name = "Bar"};