// since 8.0 public void StaticFunctions() { var instance = new UsefulObject(); instance.Name = "Matthew"; instance.Age = 26; var result = Calculate(instance);
static int Calculate(UsefulObject inst) { if (inst.Name == "Matthew") { return(inst.Age); } return(-1); }
public void Demonstrate(UsefulObject instance) { // get it or throw int justGetIt = instance.Name !.Length; // get it or null int?tryGetIt = instance.Name?.Length; // try get it or use 0 int fixIt = instance.Name?.Length ?? 0; if (instance.Name == null) { return; } // at this point, Name can't be null int magic = instance.Name.Length; }
// since 7.0 public void LocalFunctions() { var instance = new UsefulObject(); instance.Name = "Matthew"; instance.Age = 26; var result = Calculate(); int Calculate() { // Oh dear, we captured obj if (instance.Name == "Matthew") { return(instance.Age); } return(-1); } }