I am currently rewriting parts of a custom RPC engine (which cannot be replaced with anything else, so don't suggest this ;-)). Call arguments are collected in a user collection that uses an internal dictionary. There is a method T Get<T>(string)for getting a named argument. For optional arguments, I wanted to add a method TryGet<T>(string)that returns an argument or null, if it does not exist, so that the calling code can provide a default value using the zero coalescence operator. Of course, for the value type, this does not work, but instead I could use T?what I want.
So, I have this:
public class Arguments
{
// lots of other code here
public T TryGet<T>(string argumentName) where T : class
{
// look up and return value or null if not found
}
public T? TryGet<T>(string argumentName) where T : struct
{
// look up and return value or null if not found
}
}
:
return new SomeObject(
args.TryGet<string>("Name") ?? "NoName",
args.TryGet<int>("Index") ?? 1
);
, ( , ). , TryGet .
- , ?