Вопрос

We are running a program in a loop on an enterprise scheduler throughout the day. The scheduler needs an integer return value, so we have to write the program to naturally return this. However, we want to keep a running tally of some information so I need to somehow pass in a collection object, retain it's values, add the new counts to the object, and then pass the new totals back to the program that called it.

I'm relatively new to using the out keyword so I might not even be going down the right path. Right now I am being told I need to assign control of the collection object before I can use it, however this will blow away any of the counts that it contains when it's passed in. Is there a way to use out to keep the values or should I use another method?

Это было полезно?

Решение 2

collection objects, like List<T> are reference types, so you don't even need to use the out keyword if you want your method to be able to change the content of the List<T> that you pass in

Другие советы

An out parameter doesn't logically have any value when it's passed in. Indeed, you can use a variable which isn't even definitely assigned:

int x; // No logical value yet
Foo(out x);

It sounds like you might want ref instead - although personally I try to avoid using ref or out, preferring to return all the results of my method using the return value where possible.

See my article on parameter passing for more about ref and out in general though.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top