-1

I have below array of type object

object[] myarray = new object[] { "1", "Success"};

I want to insert string value at first position in myarray is there any method to do this result should be like below:

I want array like below

object[] myarray = new object[] { "logFile","1", "Success"};
ND's
  • 2,155
  • 6
  • 38
  • 59
  • 1
    Possible duplicate of [Adding values to a C# array](https://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array) – Timothy Groote Jul 26 '17 at 10:01

2 Answers2

3

If you need to stick with the array:

myarray = new object[]{"logFile"}.Concat(myarray).ToArray();

The classic, non-LINQ (and most efficient) way is to use Array.Copy:

var newArray = new object[myarray.Length + 1];
newArray[0] = "logFile";
Array.Copy(myarray, 0, newArray, 1, myarray.Length);
// myarray = newArray;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Or the other way round: `myarray = myarray.Concat(new object[] {"logFile"}).ToArray();`. – Ian H. Jul 26 '17 at 10:08
  • @Tim your solution is good but is complicated for a beginner :) – BRAHIM Kamel Jul 26 '17 at 10:09
  • 1
    @BRAHIMKamel Using (basic) Linq is not complicated at all. This is simply a function that merges 2 arrays together, converting to a list and then inserting sounds much more complicated in my opinion. – Ian H. Jul 26 '17 at 10:10
  • @BRAHIMKamel; you find the LINQ way complicated? Then look at the classic way using `Array.Copy` i have asdded now ;-) – Tim Schmelter Jul 26 '17 at 10:12
  • Ok I know this is the main Goal of LINQ do more with less code but the OP will escape there is a built in class for this – BRAHIM Kamel Jul 26 '17 at 10:12
  • @TimSchmelter I'm not saying is complicated for me I enjoy LINQ and the great idea behind it but for sake of KISS principle :) – BRAHIM Kamel Jul 26 '17 at 10:14
1

You can switch to List<string> which can grow dynamically

  List<string> myArray= new List<string> { "1", "Success"};

and use List.Insert

myArray.Insert(0,"LogFile"); 

From MSDN how List.Insert works

If Count already equals Capacity, the capacity of the List is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added. If index is equal to Count, item is added to the end of List.

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • what exception you got – BRAHIM Kamel Jul 26 '17 at 10:02
  • yes there is an insert https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.Collections.Generic.List%601.Insert);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.2);k(DevLang-csharp)&rd=true – BRAHIM Kamel Jul 26 '17 at 10:03
  • 1
    You should rename `myArray` to `myList` ... the names are pretty confusing when you have a List named `myArray`. @John make sure that you call the Insert method on a `System.Collections.Generic.List` and not an array. – Ian H. Jul 26 '17 at 10:03
  • @IanH. yes you are right but just for sake of simplicity I named it array – BRAHIM Kamel Jul 26 '17 at 10:04
  • List myArray1 = File.ReadAllLines(ScriptPath + "\\executionresult.txt"); – ND's Jul 26 '17 at 10:05
  • @John try this: `List myArray1 = File.ReadAllLines(ScriptPath + "\\executionresult.txt").ToList();`. You have to convert the array to a list in order to save it as such. – Ian H. Jul 26 '17 at 10:06
  • @John what is your question ? – BRAHIM Kamel Jul 26 '17 at 10:06
  • @BRAHIMKamel He is **saving the array into a list variable**. – Ian H. Jul 26 '17 at 10:07
  • `yes you are right but just for sake of simplicity I named it array` For the sake of simplicity I could call my cat a dog. But that isn't helpful. It isn't a dog. – mjwills Jul 26 '17 at 10:12
  • Thanks Brahim for your answer – ND's Jul 26 '17 at 10:13