Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
the function array i had created but but its repeat the function
when i add 1 to integer array the the result is 1
and when i call a function A gain for Example add number 2 output is
1
1
2

and I need function to RemoveAt but I Can't Create

What I have tried:

string[] FirstArray = new string[0];

public void Add(string item)
{


string[] NewArray = new string [FirstArray.Length+1];
int i;
for (i = 0; i < FirstArray.Length; i++)

{

NewArray[i] = FirstArray[i];


}




FirstArray = NewArray;

foreach (var x in FirstArray)
{

Console.WriteLine(x);


}
Posted
Updated 23-Dec-21 8:57am
Comments
Richard MacCutchan 23-Dec-21 10:13am    
What is the problem?
ahmedbelal 23-Dec-21 10:27am    
Hi Mr Richard Nice To meet You , i want to Create Function To add element to array C# Without Using built-in Function , but i have Error in logic the function work First time the Outbut is 1 but when i calling function a gain and add second number 2 The Out but is 1 1 2
BillWoodruff 24-Dec-21 4:37am    
If this is an assignment, you need to clarify what the assignment actually is.

An assignment to create a structure that emulates a C# Array, but provides Add/Remove functions makes no sense: if you remove an element the indexes may no longer point to the elements they were originally accessed by.

Quote:
Please I want to Learn How to Create Function To RemoveAt element from Array Without Using built-in Function

The array in C# is not a "resizable" object - to genuinely add or remove an item you need to create a new array and fill it with the required data. That's actually not an efficient thing to do, and you are much much better off using the built in List<T> class which encapsulates an array allowing Insert and Remove operations to be as efficiently as possible.

This may help: List<T> - Is it really as efficient as you probably think?[^]
It's also worth checking the reference source: Reference Source: Generic List[^]
 
Share this answer
 
Comments
ahmedbelal 23-Dec-21 12:32pm    
You Are right but i need it just For deep Learning
OriginalGriff 23-Dec-21 12:37pm    
Then follow the reference source - it will show you how Microsoft does it.
ahmedbelal 23-Dec-21 12:37pm    
For Example int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
I want to Learn How to Create Function To RemoveAt element from This Array Without Using built-in Function
Really Iam Pleasure to Learning from You Dear Friend OriginalGriff
BillWoodruff 23-Dec-21 14:45pm    
+5 at last the voice of reason speaks :)
In your add method, you never actually add anything to the new array:
C#
string[] NewArray = new string [FirstArray.Length+1];
int i;
for (i = 0; i < FirstArray.Length; i++)
{
    NewArray[i] = FirstArray[i];
}
// You need to add the new item here
NewArray[i] = item;
 
Share this answer
 
Comments
ahmedbelal 23-Dec-21 10:33am    
Of Course i do that Now thanks , but i have Error in logic the function work First time the Outbut is 1 but when i calling function a gain and add second number 2 The Out but is 1 1 2 Why Mr Richard MacCutchan
Richard MacCutchan 23-Dec-21 10:41am    
Because you are printing all the items in the array each time. So move the printing code somewhere else so it is only executed after you have added all the items.
ahmedbelal 23-Dec-21 12:14pm    
Done Thanks Really Thanks Really nice to meet you
Please I want to Learn How to Create Function To RemoveAt element from Array Without Using built-in Function
Richard MacCutchan 23-Dec-21 12:25pm    
It is just the reverse of the add method. Create a NewArray that is one smaller than FirstArray. Then copy all the elements except the one that you want to remove. You can use the actual value or the position in the array to indicate which item to remove.
ahmedbelal 23-Dec-21 12:37pm    
For Example int[] array = { 1, 2, 3, 4, 5, 6, 7, 8 };
I want to Learn How to Create Function To RemoveAt element from This Array Without Using built-in Function
Really Iam Pleasure to Learning from You
To both echo, and amplify, OriginalGriff's reply:

0) burn this into your brain:) C# Arrays are fixed length entities: you cannot add to them, or delete an element in them

1) you allocate the size of the Array when you create it

2) you can: change/replace a value of an array element

Everything you say you want to do can be handled easily by using a generic List which offers Add, Remove, Clear, etc. methods: [^]
 
Share this answer
 
v2
Comments
Richard MacCutchan 24-Dec-21 4:16am    
It's a school/college assignment which specifically instructs them not to use built-ins, but to write the code themselves.
BillWoodruff 24-Dec-21 4:31am    
Yes, a possible hypothesis, but, i think we don't have enough information to make that a probable hypothesis. imho, the most probable hypothesis is that the OP is confused.

I've added another comment to the OP's post.
to try and get them to clarify,

Merry Xmas :)
Richard MacCutchan 24-Dec-21 4:55am    
Not a hypothesis at all, it is clearly stated in the question, and in various comments by the OP.

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