Tuesday, 15 May 2012

c# - How do I convert to the type IList? -


I want to get effects of inserting random numbers in a IList and it looks like I am unable to think about how to get the type of iialit , regardless of a int or decimal , etc. I just type in Console.ReadLine () in the IList . Public Zero Randomizeilist & lt; T & gt; (IList & lt; T & gt; List) {randomNum = new Random (); T-typered = 0, type read = 0; String strRead = "", strReadSeed = ""; Console.light line ("How many {0} are you randomly generated?", List.GetType ()); T strRead = (list.GetType ()) console. Readline (); Console.light line ("What is the limit of randomly generated {0} s?", List. GetType ()); Int32.TryParse (strReadSeed, intReadSeed out); For (int i = 0; i

Syntactically what you want:

  T strRead = (T) console Readline ();   

However, Console.ReadLine only returns a string, so this artist (and the use of generics) does not make any sense . Either you have to IList & lt; String & gt; should be used (because you think that the T is a string) or you IList & lt; Int & gt; because you are adding int to the list). strRead .

With no updates, per comment , because in any case, it is not very clear what you are trying to do:

Of course, you can convert a string to an arbitrary type. Some settings for this framework, such as convert class:

  convert t strRead = (T) (object). ChangeType (Console.ReadLine ()) provides Is, typeof (t));   

This will work for simple types - you convert convert to a string to a int , For example. However, it should go without saying that you can not use this class to convert any arbitrary string into any arbitrary type. For this, you have to consider your own type of conversion structure, possibly with the implicit and obvious conversions, adding the behavior of convert etc. This is because it is clear that a particular type of string is represented entirely dependent on that type of characteristics.

No comments:

Post a Comment