Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting C# out parameters?

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

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

Roughly speaking (and if I didn't have static typing) I want to do:

dict.TryGetValue(key, out value); 

but this obviously won't compile because it "cannot convert from 'out string' to 'out object'".

The workaround I'm using is:

object valueAsObject; dict.TryGetValue(key, out valueAsObject); value = (string) valueAsObject; 

but that seems rather awkward.

Is there any kind of language feature to let me cast an out param in the method call, so it does this switcheroo for me? I can't figure out any syntax that'll help, and I can't seem to find anything with google.

like image 641
Ken Avatar asked Aug 27 '09 20:08

Ken


People also ask

What is casting in C?

Type Casting is basically a process in C in which we change a variable belonging to one data type to another one. In type casting, the compiler automatically changes one data type to another one depending on what we want the program to do.

Does C have casting?

C also allows the programmers to do type casting. Typecasting and Type conversion are different things. In C, typecasting is a way to simply change the data type of a variable to another data type. Typecasting is so useful and efficient.

What happens when casting in C?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.

What is type casting syntax?

Type casting is when you assign a value of one primitive data type to another type. In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size. byte -> short -> char -> int -> long -> float -> double.


2 Answers

I don't know if it is a great idea, but you could add a generic extension method:

    static bool TryGetTypedValue<TKey, TValue, TActual>(         this IDictionary<TKey, TValue> data,         TKey key,         out TActual value) where TActual : TValue     {         TValue tmp;         if (data.TryGetValue(key, out tmp))         {             value = (TActual)tmp;             return true;         }         value = default(TActual);         return false;     }     static void Main()     {         Dictionary<string,object> dict             = new Dictionary<string,object>();         dict.Add("abc","def");         string key = "abc", value;         dict.TryGetTypedValue(key, out value);     } 
like image 161
Marc Gravell Avatar answered Oct 06 '22 01:10

Marc Gravell


I spy with my little eye an old post that was still active a month ago... Here's what you do:

public static class DictionaryExtensions {     public static bool TryGetValueAs<Key, Value, ValueAs>(this IDictionary<Key, Value> dictionary, Key key, out ValueAs valueAs) where ValueAs : Value     {         if(dictionary.TryGetValue(key, out Value value))         {             valueAs = (ValueAs)value;             return true;         }          valueAs = default;         return false;     } } 

And because compilers are great, you can just call it like this:

dict.TryGetValueAs(key, out bool valueAs); // All generic types are filled in implicitely! :D 

But say you're not creating a blackboard AI and just need to call this operation the one time. You can simply do a quicksedoodle inliner like this:

var valueAs = dict.TryGetValue(key, out var value) ? (bool)value : default; 

I know these answers have been given already, but they must be pretty old because there is no cool hip modern inlining going on to condense these methods to the size we really want: no more than 1 line.

like image 43
Casey Hofland Avatar answered Oct 06 '22 03:10

Casey Hofland