Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Based on my functionality I want convert int[] to object[]. I tried to do this but I can't to convert this.

example:
C#
int[] intrs = new int[] { 400, 435, 446, 900 };

            object[] obj = intrs;



Can you any one tell me how to convert int[] to object[]?

thanks in advance..
Posted
Updated 15-Jun-21 4:58am

There is a very nice explanation for the issue here that you should read - Can't convert value type array to params object[][^]
 
Share this answer
 
Comments
BillWoodruff 13-Dec-13 4:54am    
Upvoted: reading Eric Lippert is always a good thing !
Ankur\m/ 16-Dec-13 4:48am    
Indeed! :)
C#
int[] intrs = new int[] { 400, 435, 446, 900 };

            object[] obj = (from i in intrs select i).Cast<object>().ToArray();

it will help you use this casting method
 
Share this answer
 
v3
Comments
U@007 13-Dec-13 5:00am    
thank you kukana
:)
Omprakash Kukana 13-Dec-13 6:14am    
your welcome 007
You can't convert an array that simply - you can't explicitly cast it either. What you have to do is create a new object[] from the existing data.
It's pretty easy though:
C#
int[] intrs = new int[] { 400, 435, 446, 900 };
object[] obj = Array.ConvertAll(intrs, x => (object)x);
 
Share this answer
 
Comments
[no name] 13-Dec-13 4:39am    
Really a good solution in lambda expression
U@007 13-Dec-13 4:43am    
thank you originalgriff :) 5+
OriginalGriff 13-Dec-13 4:44am    
You're welcome!
BillWoodruff 13-Dec-13 4:53am    
+5 Bullseye.
Be explicit:

int[] intrs = new [] { 400, 435, 446, 900 };

object[] obj = intrs.Select(x => x as object).ToArray();
 
Share this answer
 
Comments
Richard Deeming 15-Jun-21 12:04pm    
Exactly the same as the code posted in solution 3 eight years ago.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900