-1

I am coming from Java and C++ background. However, I am doing a C# application at the moment and one thing made me confused.

In Java when I import a package, it means I can access the classes that are placed in that package. For example, here I imported the ArrayList class from package java.util.

import java.util.ArrayList;

class ArrayListUtilization {
    public static void main(String[] args) {

        ArrayList<Integer> myList = new ArrayList<>(3);
        myList.add(3);
        myList.add(2);
        myList.add(1);

        System.out.println(myList);
    }
}

Recently I had a problem in my c-sharp app, that I asked here. The funny part in my point of view is, when I added the following snippet of code:

var accountDbContext = services.GetRequiredService<AccountDbContext>();
accountDbContext.Database.EnsureCreated();
var accountDbCreator = accountDbContext.GetService<IRelationalDatabaseCreator>();
accountDbCreator.CreateTables();

I saw an error as following:

enter image description here

Error CS1929 'AccountDbContext' does not contain a definition for 'GetService' and the best extension method overload 'ServiceProviderServiceExtensions.GetService(IServiceProvider)' requires a receiver of type 'IServiceProvider'

and from the error text, I understood accountDbContext object does not have GetService function. But when I press on show potential fixes, then it suggests me to add a using statement.

enter image description here

And it was the real fix. However, my question is what is the effect of this using statement on my object? The object is an instantiation of its class. How can adding a using statement effect on my object and add a function to it?

MJBZA
  • 4,796
  • 8
  • 48
  • 97
  • Using is like import in Java, I am from Java background and I switched to C# well a few methods need importing (as you may already know it) or you can do it like :Microsoft.EntitiyFrameworkSomethingSomething.Infra.Method.Function (pretty long) and that is an uneasy way of doing something in C# maybe ur class needs it? I am not sure –  Feb 19 '21 at 08:45
  • Maybe ur class needs it I am not sure –  Feb 19 '21 at 08:47
  • 1
    [What are extension methods?](https://stackoverflow.com/questions/403539/what-are-extension-methods) (maybe) – ProgrammingLlama Feb 19 '21 at 08:47
  • What is the relation then? I can access my object. This using will remove the error of missing function. – MJBZA Feb 19 '21 at 08:48
  • @John Thanks, I will follow up that keyword. – MJBZA Feb 19 '21 at 08:49

1 Answers1

4

Note that what you are actually calling an extension method here:

accountDbContext.GetService<IRelationalDatabaseCreator>();

accountDbContext does not have a method called GetService. GetService is declared in AccessorExtensions, and the above line is just syntactic sugar for:

AccessorExtensions.GetService<IRelationalDatabaseCreator>(accountDbContext);

Now it should make sense that you need to add a using directive for the class in which the extension method is declared, in order to access the extension method.

Sweeper
  • 213,210
  • 22
  • 193
  • 313