Example #1
0
    static void Main(string[] args)
    {
        var item = new MyType();
        // Option 1 - Implicit cast. Compile time checked but takes two lines.
        IMyType item2 = item;

        System.Console.WriteLine(item2.SayHello());
        // Option 2 - One line but risks an InvalidCastException at runtime if MyType changes.
        System.Console.WriteLine(((IMyType)item).SayHello());
        // Option 3 - One line but risks a NullReferenceException at runtime if MyType changes.
        System.Console.WriteLine((item as IMyType).SayHello());
        // Option 4 - compile time one liner
        Converter.ReturnAs <IMyType>(item).SayHello();
    }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              IApplicationLifetime appLifetime, IMyType myType)
        {
            myType.SayHello();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }