How to call an extension method without using

后端 未结 7 1184
旧时难觅i
旧时难觅i 2021-01-03 22:37
using System;

class Runner
{
    static void Main()
    {
        A a = new A();
        // how to say a.PrintStuff() without a \'using\'
        Console.Read();
           


        
7条回答
  •  灰色年华
    2021-01-03 23:03

    In our projects extensions are placed in the same namespace as class extension for. Your example:

    A.cs:
    using System;
    namespace ANamespace
    {
    
         class A { }
    }
    
    AExtensions.cs:
    namespace ANamespace
    {
        static class AExtensions
        {
            public static void PrintStuff(this A a)
            {
                Console.WriteLine("text");
            }
        }
    }
    

    Now when you add using for ANamespace for using the A class, all extensions for A class will be included too.

提交回复
热议问题