E
E
Evgeny Mikhalev2018-06-24 08:22:41
C++ / C#
Evgeny Mikhalev, 2018-06-24 08:22:41

What is this construct public T GetService() where T: class { throw new NotImplementedException(); } in C#?

I'm trying to figure out the program in C #, but I don't know exactly Sharps, and I didn't write for Win.
what kind of construction is this in general,
and where you can read about what it is:
1) GetService
2)

T
        public T GetService<T>() where T : class
        {
            throw new NotImplementedException();
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2018-06-24
@lexxpavlov

This is a generic method declaration where the body is not defined.
There are three different concepts in this code:
1) this is a declaration of the GetService method with no arguments and with a return type T
2) The return type is T - this is a generic type, the real type will be specified when the method is called . The where word specifies a constraint - the real type must be a class.
For example, you can specify MyService x = GetSevice<MyService>();either Person x = GetSevice<Person>();or IWeapon x = GetSevice<IWeapon>();.
3) as the body of the method is - an exception will occur indicating that the body is not defined. This is done if it is necessary to show that this code does not need to be called, or if the body is written later, and now there will be an exception when called. throw new NotImplementedException();

M
Matvey Pravosudov, 2018-06-24
@oxyberg

T— placeholder for the type. And about the design on the Microsoft website .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question