How do I override ToString() and implement generic?

前端 未结 4 1267
温柔的废话
温柔的废话 2021-01-16 09:33

I have code that I want to make the following changes:

  1. How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)\'

4条回答
  •  野的像风
    2021-01-16 10:13

    If you want to override ToString(), you would need to inherit from List rather than try to extend it. You have already seen that you cannot mark the static extension method as override, and overload resolution will always go for the member method over an extension method if it is available. Your options are

    • Inherit and override
    • Change your extension method's name to something else ToSpecialString()
    • Call the method directly using the class name MyExtensions.ToString(myList);

提交回复
热议问题