Saturday, August 29, 2026

C# Default Method implementation in interface

Since C#8, you can define a method with its implementation in interface. The question is: when can you use this default implementation. The answer is : you can use default implementation as long as re-implementation of method doesn't exist in the implementing class of the interface. If a re-implementation exists, the default method implementation is never used; it becomes useless. So, defining a default method implementation in interface makes sense if you want a uniform method definition for all the classes that implement the interface. They will have a common method definition, if interface defines a method.

Look at the following code.

namespace Practicals
{
    interface IMyInterface
    {
        void DoSomething()
        {
            Console.WriteLine("Default implementation");
        }
    }

    class MyClass : IMyInterface
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();

            IMyInterface iobj = obj;
            iobj.DoSomething(); // Default implementation
        }
    }
}
The output is: Default implementation
What if the MyClass re-implements the DoSomething method?

namespace Practicals
{
    interface IMyInterface
    {
        void DoSomething()
        {
            Console.WriteLine("Default implementation");
        }
    }

    class MyClass : IMyInterface
    {
        public void DoSomething()
        {
            Console.WriteLine("Re implementation");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.DoSomething(); // Re implementation
            IMyInterface iobj = obj;
            iobj.DoSomething(); // Re implementation
        }
    }
}

The output is: Re implementation

The default implementation of interface is never used if a re-implementation exists in the implementing class. No matter you call using a reference variable of implementing class or interface, the default implementation of interface is never; the re-implementation of interface is used.

Wednesday, July 1, 2026

C# generic method with or without method parameter

In C#, a generic method is a method that is declared with a type parameter (usually denoted as <T>). However, when the method is called then type argument for type parameter is optional or not, is explained in this post.

Here is a breakdown of generic methods with and without method parameters, followed by the core differences.

Generic Method WITH Method Parameters

In this scenario, the type parameter <T> is used as the data type for one or more of the arguments passed into the method.

Example

public class Utility
{
    // The type parameter T is used in
    // the method parameter (T input)
    public void PrintDetails<T>(T input)
    {
        Console.WriteLine($"Type: " +
            $"{typeof(T)}, Value: {input}");
    }
}

How you call it:

Because the compiler can see what you are passing into the method, it can automatically figure out what T is. This is called Type Inference.

class Program
{
    static void Main(string[] args)
    {
        Utility util = new Utility();

        // 1. Calling WITHOUT explicitly
        // specifying the type (Type Inference)
        util.PrintDetails("Hello World"); // Compiler infers T is string
        util.PrintDetails(123); // Compiler infers T is int

        // 2. Calling WITH explicitly specifying the type
        util.PrintDetails<double>(45.67);
    }
}

Generic Method WITHOUT Method Parameters

In this scenario, the type parameter <T> is used inside the method body or as the return type, but it does not appear in the method's input parameters.

Example

public class Factory
{
    // T is the return type, but 
    // the method takes no arguments
    public T CreateInstance<T>() where T : new()
    {
        return new T();
    }
}

How you call it:

Because there are no arguments being passed, the compiler has zero clues to figure out what T should be. Therefore, you must explicitly provide the type parameter.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Factory factory = new Factory();

        // You MUST explicitly state <Person> or <List<int>>
        Person p = factory.CreateInstance<Person>();
        var list = factory.CreateInstance<List<int>>();

        // THIS WILL FAIL TO COMPILE:
        // Error: Type arguments cannot be inferred
        // var item = factory.CreateInstance(); 

    }
}

Key Differences
Feature With Method Parameter (Method<T>(T arg)) Without Method Parameter (Method<T>())
Type Inference Supported. The compiler looks at the argument passed to determine T. Not Supported. The compiler has no input data to analyze.
Calling Syntax Clean and implicit: Method(argument) Explicit: Method<Type>()
Primary Use Case Processing, comparing, or operating on data of a flexible type (e.g., swapping two values, logging an object). Creating instances, fetching configuration, or casting data (e.g., Dependency Injection containers, ORMs like Entity Framework).

Summary

  • Use with method parameters when the method needs to ingest and act upon generic data. You get the benefit of cleaner code via type inference.
  • Use without method parameters when the method needs to generate, fetch, or convert something into a specific type from scratch. You must explicitly tell the compiler what type you want.

Hot Topics