Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for example`
DynamicArray d = new DynamicArray(4, 5, 6, 1, 2);
when i will call GetLength function, it will print 5.
Console.WriteLine(d.GetLength());
It will print "Number of elements are 5"

What I have tried:

I wrote this function`

public int GetLength(params int[] x)
{
// I don't know what write here.
}
Posted
Updated 18-Jan-18 5:18am

Try:
C#
public int GetLength(params int[] x)
    {
    return x.Length;
    }
 
Share this answer
 
Comments
Thomas Daniels 18-Jan-18 11:19am    
Beat me to it! :laugh: +5
OriginalGriff 18-Jan-18 11:30am    
I *knew* that typing course would come in handy! ;)
Suren97 18-Jan-18 11:23am    
Thank you very much :)
and please can you say how can i remove random element from that array?
OriginalGriff 18-Jan-18 11:30am    
You can't remove elements from a standard array at all - once it's allocated, it can't change size. I've not heard of a class called DynamicArray, but there is the List class which is flexible and will allow you to Insert and Delete elements, as well as access elements via an index just as if it was an array (which it is, internally - it wraps an array).

But...if you are regularly inserting or deleting elements (from a List or anything similar) you might want to read this:
https://www.codeproject.com/Articles/870013/List-T-Is-it-really-as-efficient-as-you-probably-t
There are performance concerns if the element count gets high.
I don't know what DynamicArray is, but to complete the code snippet you have (getting the length of an integer array), you only have to return x.Length:
C#
public int GetLength(params int[] x)
{
    return x.Length;
}
 
Share this answer
 
Comments
Suren97 18-Jan-18 11:23am    
Thank you very much :)
and please can you say how can i remove random element from that array?
Thomas Daniels 18-Jan-18 11:26am    
You can't remove elements from arrays, but you can use a List<int>, then generate a random integer (that's a valid index of the List) and then use the RemoveAt method: see the documentation of List<T> and Random.
Suren97 18-Jan-18 11:28am    
for example i want remove number 6, i can't?
Thomas Daniels 18-Jan-18 11:28am    
Not with an array; but you can use a List<int> and then you can do .Remove(6) on the list.
Suren97 18-Jan-18 11:30am    
if not with an array, for example with what?

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