Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have Available room in

IEnumerable<int> p

and I want to them in Array m[raj]

I am using following code but it gives an error

C#
for (int raj = 0; raj < m.Length; raj++)
      {
       m[raj] = p[raj];
      }
Posted
Updated 27-May-15 1:01am
v3
Comments
King Fisher 27-May-15 7:01am    
you cant change array size on run time.so, You can use Array List.

Arrays have a predefined size that cannot change. You could use Array.Resize (not the best option performance wise) or use a List and call ToArray on that when you are done to get an actual array (if really needed). Check the links for more info/.

https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx[^]

https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^]

Good luck!
 
Share this answer
 
It's not quite clear what the actual problem here is. So here are some suggestions:

The IEnumerable-interface doesn't have an indexer. For p[raj] to work, p would have to be not a "plain" IEnumerable but an array or a List or something else that allows indexing. Otherwise you would have to use ElementAt(..) to get the element of an IEnumerable at a specified position:
C#
m[raj] = p.ElementAt(raj);

(This has potentially a really bad performance because it needs to iterate the elements of the IEnumerable to find the n'th one.)

You can just call ToArray() on any IEnumerable to get an array with the same elements:
C#
var m = p.ToArray();

If you want to limit the amount of elements from p that will be "put" into the array (as you seem to with your for-loop-predicate) you can use Take(..) to take just the first X elements:
C#
int amount = ...
var m = p.Take(amount).ToArray();


If you can't solve your issue with these hints, please leave a comment and tell the actual error message you're getting.
 
Share this answer
 
Comments
Shrikesh_kale 28-May-15 0:23am    
thanks ..
Sascha Lefèvre 28-May-15 4:53am    
You're welcome!

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