1

I have a method that accepts a generic type of BaseViewModel. I want to get the actual object in the method.

What I tried:

public static void LogScreen<T>() where T : BaseViewModel
 {
    var viewModel = T as BaseViewModel;
 }
Lennie
  • 2,429
  • 2
  • 18
  • 16
  • 8
    "the actual object", what do you mean? If you need an instance of an object, take it as a parameter. `void LogScreen(T instance) ...` – Lasse V. Karlsen Jan 02 '18 at 07:25
  • 1
    Very unclear what you are looking for, could be duplicate of `new T()` ? - https://stackoverflow.com/questions/11234452/c-sharp-create-object-obj-new-t – Alexei Levenkov Jan 02 '18 at 07:31
  • There is something wrong with your idea or architecture if you need to get instance of defined type in generic function. Even if you are using "where T". You have a base type accepting by this function so you should use "public static void LogScreen(BaseViewModel baseViewModel)" instead. – Anton Anpilogov Jan 02 '18 at 08:01

1 Answers1

7

You are not passing any object into the method, only the parameter type.

To apply value correctly, You should use the syntax like:

public static void LogScreen<T>(T TArg)
    where T : BaseViewModel
{
    var viewModel = TArg; //not necessary: as BaseViewModel;
}

In this kind of code You do not really need to use generic and You might go with simplier version as:

public static void LogScreen(BaseViewModel viewModel)
{
     //already got viewModel as correct type and checked during compile for types
}

Note: The big advantage of using generic approach is putting there some type and then work with the type whole time (great example IEnumerable<int>). If You would do that without generic type, You would need to use object everywhere and You would need to cast the object all the time

  • Generic is advantage there as You can put anything inside.

On the other hand if You have a method, where You have only one type the only one, it is easier to go with strict parameter type (2nd approach).


Comment of ZoharPeled - the code should be written as:

public static void LogScreen<T>(T TArg)
    where T : BaseViewModel
{
    var viewModel = TArg as BaseViewModel;
    var viewModel2 = TArg;
}

From the compiler's point of view, the type of the reference named viewModel is exactly the same as the type of the reference named viewModel2 - both are BaseViewModel, and both instances may or may not be of any type derived from BaseViewModel. Long story short, the as operator is redundant in this code. Even if you pass an instance of some class derived from BaseModelView you can only reference it as a BaseModelView in this method.

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • `var viewModel = TArg as BaseViewModel;` should be `var viewModel = TArg;`, since `TArg` is already `BaseViewModel` or deriving from it. – Zohar Peled Jan 02 '18 at 07:49
  • @ZoharPeled replied in Q. – Tatranskymedved Jan 02 '18 at 08:14
  • 1
    Well, as much as I like to read my name in **bold** font, your edit is wrong. From the compiler's point of view, the type of the reference named `viewModel` is exactly the same as the type of the reference named `viewModel2` - both are `BaseViewModel`, and both instances may or may not be of any type derived from `BaseViewModel`. Long story short, the `as` operator is redundant in this code. Even if you pass an instance of some class derived from `BaseModelView` you can only reference it as a `BaseModelView` in this method. – Zohar Peled Jan 02 '18 at 08:21
  • @ZoharPeled alright, checked in code and I have to apologize. Again the day wasn't useless. Thanks! – Tatranskymedved Jan 02 '18 at 08:27
  • 1
    No apologies needed. Neither of us holds the absolute knowledge about c#, and I'm sure I can learn things from you just as you where able to learn something from me. I think that's what this community is all about. – Zohar Peled Jan 02 '18 at 08:41