Friday, 19 March 2021

MVC - Simplest Extension Method example

 Watch this example on YouTube


 1. Model

   public interface IExtensionTest
    {
         void DoSomething();
    }
    public class ExtensionTest: IExtensionTest
    {
        public void DoSomething() { }
    }
    public static class SomethingElse {
        public static string ReturnSomething(this IExtensionTest t, string s)
        {
            return "returning: " + s;
        }
    }


2. Controller

        public ActionResult Index()
        {
            IExtensionTest e = new ExtensionTest();
            var res = e.ReturnSomething("bbbbb");
            return View();
        }

1 comment:

  1. This simple example of extension methods in MVC is super helpful! Just like Razor host streamlines web hosting, these methods can simplify your code.

    ReplyDelete