简体   繁体   中英

Object to System.String implicit conversion

I have a question regarding type conversion in C#.


Object data_obj = "test";
string data_str;
data_str = data_obj;

this produces an error claiming no implicit conversion exists. Now, this is, at least for me, a little bit unintuitive, since Console.WriteLine("type{0}",data_obj.GetType()) produces System.String , but then again, i am really new at C#.

Now, getting past this, i would like to cast to the type of the data stored in data_obj , without having to switch and selecting the type manually.

So, basically, what i am asking is: is there a way to define an implicit conversion from System.Object to System.String , System.Decimal , etc.

Context:

I am reading from an ODBC connection to an SQLServer database and trying to pump those rows into an Oracle Database. Now, the correspondence of SQLServer and Oracle rows have to be defined. Now, i read the data from the SQLServer table with no problem whatsoever, and i store it in a List<Dictionary<string,Object>> (i know, storing the rows seem wrong, but design requirements, what can you do). The insertion part is where i am having problems with the insertion. I basically loop (with a foreach) through the elements of the list and insert them row by row. It is here where i need to determine the type of the data stored in the Object .

Edit: Including a bit more detail.

No, there's no way of doing it implicitly. Options:

  • Call ToString() explicitly (which will throw an exception if the reference is null)
  • Call Convert.ToString()
  • Use "" + data_obj (ick, don't do it)
  • Cast if you're sure that data_obj is really a string

Why are you so keen on an implicit conversion though?

What you should be doing in your code as noted above is casting , not converting . The object in question is a string; you simply have stored it in an object reference.

Casting would be done like so:

data_str = (string)data_obj;

The overload of Console.Writeline you are mentioning takes a format string, and then a params array of object ... not strings. It calls ToString() on each of those objects. So your call to data_obj.GetType().ToString() could be slimmed down to data_obj.GetType() (calling ToString() on a string just returns the string again)

As for an implicit conversion from object to type x ... think about what you are asking; a reference of object can be any CLR type. How do you handle those conversions?

You could use the Convert.ToString method:

object o = ...
string s = Convert.ToString(o);

There is no implicit conversion from object to string in C#. However you could call the object's virtual ToString method

string s = o.ToString(); 

If you store your value in a var instead of type object and let the compiler decide what the value is, then it can be made implicit.

var data_obj = "test";
string data_str;
data_str = data_obj;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM