-1

I found, that construction

Method(out List<T>list)
{
    list.Clear();      // doesn't allowed to initialyze List<T>list
    list = null;       // is accepted by VSTO, however, is not so good
}

Any suggestion please?

  • 3
    `out` parameters are generally for things that are created WITHIN the method, not for modifying things passed INTO the method. Perhaps you should be using `ref`. You need to show the context in which this code will be used. – Matthew Watson Nov 17 '16 at 13:13
  • Thanks Mattew! In fact, I'm handling into the method the list field in the class, like public Liststr = new List(); ... – Pavel Khrapkin Nov 17 '16 at 14:12

2 Answers2

4

You cannot use unassigned parameter withing this method. There is simple rule: use out whether parameter is not initialized or use ref if you pass initialized parameter to method.

This code will run correctly:

void Method<T>(ref List<T> list)
{
    list.Clear();
    list = null;
}

Read more about differencies in this question: What's the difference between the 'ref' and 'out' keywords?

Community
  • 1
  • 1
Pawel Maga
  • 5,428
  • 3
  • 38
  • 62
2

If you want to use out semantics, not ref, you have to instantiate your list:

Method(out List<T>list)
{
    list = new List<T>();
}
Sefe
  • 13,731
  • 5
  • 42
  • 55