How to explicitly discard an out argument?

后端 未结 8 956
情歌与酒
情歌与酒 2020-12-09 14:30

I\'m making a call:

myResult = MakeMyCall(inputParams, out messages);

but I don\'t actually care about the messages. If it was an input pa

相关标签:
8条回答
  • 2020-12-09 15:07

    You must pass a variable for the out parameter. You do not have to initialize the variable before passing it:

    MyMessagesType messages;
    myResult = MakeMyCall(inputParams, out messages); 
    

    Typically, you can just ignore 'messages' after the call - unless 'messages' needs disposing for some reason, such as the use of limited system resources, in which case you should call Dispose():

    messages.Dispose();
    

    If it might use a significant amount of memory and it is going to remain in scope for a while, it should probably be set to null if it is a reference type or to a new default instance if it's a value type, so that the garbage collector can reclaim the memory:

    messages = null; // Allow GC to reclaim memory for reference type.
    
    messages = new MyMessageType(); // Allow GC to reclaim memory for value type.
    
    0 讨论(0)
  • 2020-12-09 15:08

    In this case I made an generic extension method for ConcurrentDictionary that has no Delete or Remove method.

    //Remove item from list and ignore reference to removed item
    public static void TryRemoveIgnore<K,T>(this ConcurrentDictionary<K,T> dictionary, K key)
    {
        T CompletelyIgnored;
        dictionary.TryRemove(key, out CompletelyIgnored);
    }
    

    When called from an instance of ConcurrentDictionary:

    ClientList.TryRemoveIgnore(client.ClientId);
    
    0 讨论(0)
提交回复
热议问题