3 ms·
"Step" is a static-call to an extension method. This way allows your code to keep working fine (unchanged) for users that aren't profiling (i.e. 99.99% of the u
by marcgravell 15y ago
"Step" is a static-call to an extension method. This way allows your code to keep working fine (unchanged) for users that aren't profiling (i.e. 99.99% of the users).
- MatthewPhillips 15y agoI meant more the "it's ok if this is null" part. Does that comment not mean what I'm thinking it means? Because invoking a method on a null object will throw an exception.
- nick_craver 15y agoIn the case of an extension method, it's actually just syntactic sugar the compiler provides. An extension method is actually a static method underneath, this allows null checking to be centralized, and cleaner everywhere a step is used. The method looks like this: "public static IDisposable Step(this MiniProfiler profiler, string name, ProfileLevel level = ProfileLevel.Info) { return profiler == null ? null : profiler.StepImpl(name, level); }"
- bitdiffusion 15y agoExtension methods can be invoked on null objects (the object gets passed to the static method and you can check for null in the extension itself). Strange, but does enable some interesting syntactic sugar checking for empty values etc.
- MatthewPhillips 15y agoNice. I've used extension methods from time to time but not in this way. Could prevent you from writing if foo != null a lot if you have an extension method by doing: public static bool Exists(this Foo foo) { return foo != null; } So then you can say if(foo.Exists()) { foo.bar(); } Or make Exists return an instance of Foo (foo if not null, a new Foo if not) and go with foo.Exists().bar();
- Mafana0 15y agoAlthough I'm not a regular contributor, I'm pleased to see that Marc Gravell is here. Welcome aboard.