Casting C# out parameters?

后端 未结 6 1611
孤城傲影
孤城傲影 2020-12-30 20:35

Is it possible to cast out param arguments in C#? I have:

Dictionary dict;  // but I know all values are strings
string key, value;
         


        
6条回答
  •  心在旅途
    2020-12-30 20:59

    No, there is no way around that. The out parameter must have a variable that matches exactly.

    Using a string reference is not safe, as the dictionary can contain other things than strings. However if you had a dictionary of strings and tried to use an object variable in the TryGetValue call, that won't work either even though that would be safe. The variable type has to match exactly.

提交回复
热议问题