0

I had to convert a string to int array first by splitting string by comma,then convert to int array.

and following line is working fine

 int[] array = input.Split(',').Select(obj=>Convert.ToInt32(obj)).ToArray();

but i wanted to do this in different way using cast extension method.

int[] array = input.Split(',').Cast<int>().ToArray();

but using cast extension it is throwing invalid cast operation exception.

I even applied ofType() prior to cast that is not really required but still its not working.

int[] array = input.Split(',').OfType<string>().Cast<int>().ToArray();

What mistake I am making here.

I am using cast method in appropriate manner.

Yogesh
  • 3,044
  • 8
  • 33
  • 60
  • @peter please read my question first – Yogesh Nov 07 '16 at 06:59
  • For the `Cast()` method to work, the object has to be of the correct type already. The `Cast()` method only reinterprets the data, it doesn't do any conversion. See the marked duplicate for correct techniques in C# for parsing a string as an `int`. – Peter Duniho Nov 07 '16 at 06:59
  • @PeterDuniho, so it doesn't type cast, then why it is named cast – Yogesh Nov 07 '16 at 07:01
  • 1
    It does cast. But the casting operator in C# does two different things, depending on context: reinterpreting, and conversion. The method is named for the cast operator in its reinterpreting sense, not the conversion sense. Frankly, your question would best be answer by you reading the C# language documentation, to learn about the cast operator, and the MSDN documentation, to learn about the `Cast()` method. All the answers are in there. – Peter Duniho Nov 07 '16 at 07:02
  • And note that _even if the `Cast()` method did support the conversion sense of casting in C#_, it still wouldn't work because there's no explicit conversion defined from `string` to `int`. You have to use a parsing method regardless (e.g. `int.Parse()`). – Peter Duniho Nov 07 '16 at 07:04
  • @PeterDuniho if it were to be doing type casting, then it could have used int.parse internally and returning the same. Anyhow, I got my problem. I perceived it as casting method rather it is similar to OfType(). only exception it throws exception if set doesn't all the item of same type. – Yogesh Nov 07 '16 at 09:37
  • You are completely missing the point. The word "cast" has multiple meanings in programming. You are stuck on just one meaning, and ignoring the fact that even in that meaning of the word, neither the language nor the framework has no mechanism for doing what you want. The method is well-named, and does exactly what the name suggests it does. – Peter Duniho Nov 07 '16 at 16:13
  • @PeterDuniho I am sorry if I am looking adamant to you. I admit I had misconception regarding this cast word and it may be the case with other dumb people like me as well as because not everybody is geek or intelligent like you. So, why don't you put your comment as answer. So, in future someone has same misconception, then it will clear their doubts. thanks Peter for valuable support. – Yogesh Nov 08 '16 at 04:49
  • You can also use following method: int[] array = Array.ConvertAll(input.Split(','), Convert.ToInt32); – Dmitry Shashurov Aug 22 '22 at 17:33

2 Answers2

1

You should use int.Parse to convert a string to int. In C#, a string is not being able to be casted directly into integer.

input.Split(',').Select(int.Parse).ToArray()
Niyoko
  • 7,512
  • 4
  • 32
  • 59
  • what is the mistake cast method of course I do have a working query already. Why I can't use cast method – Yogesh Nov 07 '16 at 06:57
0

You can also use following method.

int[] array = Array.ConvertAll(input.Split(','), int.Parse);
Shell
  • 6,818
  • 11
  • 39
  • 70
  • Of course, I can my question what is wrong with Cast method – Yogesh Nov 07 '16 at 07:02
  • @YogeshJoshi `Cast` will not work in your case. `Cast` is the extended method of `IEnumerable` interface and the type of your string array is `IEnumerable`. for example if you cast the array to enumerable by using `AsEnumerable` method then you will see the type of the array will be `System.Collections.Generic.IEnumerable` – Shell Nov 07 '16 at 07:28