Created
December 11, 2013 22:36
-
-
Save lsauer/7919764 to your computer and use it in GitHub Desktop.
C#: Merging,Appending, Extending two arrays in .NET (csharp, mono)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//www.technical-programming.com, lorenz lo sauer, 2013 | |
//description: extending C# for fast and easy string extension | |
//note: part of a larger Open-Source Project-Codebase | |
//see: http://stackoverflow.com/questions/59217/merging-two-arrays-in-net | |
//resides in IEnumerableStringExtensions.cs | |
public static class IEnumerableStringExtensions | |
{ | |
public static IEnumerable<string> Append(this string[] arrayInitial, object arrayToAppend) | |
{ | |
string[] ret = new string[arrayInitial.Length + arrayToAppend.Length]; | |
arrayInitial.CopyTo(ret, 0); | |
arrayToAppend.CopyTo(ret, arrayInitial.Length); | |
return ret; | |
} | |
} | |
//example usage | |
var someStringArray = new[]{"a", "b", "c"}; | |
var someStringArray2 = new[]{"d", "e", "f"}; | |
someStringArray.Append(someStringArray2 ); //contains a,b,c,d,e,f |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//www.technical-programming.com, lorenz lo sauer, 2013 | |
//description: extending C# for fast and easy string extension | |
//note: part of a larger Open-Source Project-Codebase | |
//resides in IEnumerableStringExtensions.cs | |
public static class IEnumerableStringExtensions | |
{ | |
public static IEnumerable<string> Append(this string[] arrayInitial, object objectToAppend) | |
{ | |
string[] objectArray = null; | |
if(!(objectToAppend is string) && (objectToAppend is System.Collections.IEnumerable)){ | |
objectArray = (string[])objectToAppend; | |
}else if(objectToAppend is string){ | |
objectArray = new string[]{(string)objectToAppend}; | |
}else{ | |
throw new ArgumentException("No valid type supplied."); | |
} | |
string[] ret = new string[arrayInitial.Length + objectArray.Length]; | |
arrayInitial.CopyTo(ret, 0); | |
objectArray.CopyTo(ret, arrayInitial.Length); | |
return ret; | |
} | |
} | |
//example usage | |
var someStringArray = new[]{"a", "b", "c"}; | |
var someStringArray2 = new[]{"d", "e", "f"}; | |
someStringArray.Append(someStringArray2 ); //contains a,b,c,d,e,f | |
someStringArray.Append("test"); //contains a,b,c,test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//www.technical-programming.com, lorenz lo sauer, 2013 | |
//description: extending C# for fast and easy string extension | |
//note: part of a larger Open-Source Project-Codebase | |
//resides in IEnumerableGenericExtension.cs | |
public static class IEnumerableGenericExtension | |
{ | |
public static IEnumerable<T> Append<T>(this T[] arrayInitial, T[] arrayToAppend) where T : System.Collections.IEnumerable | |
{ | |
if (arrayToAppend == null) { | |
throw new ArgumentNullException("The appended object cannot be null"); | |
} | |
if ((arrayInitial is string) || (arrayToAppend is string)) { | |
throw new ArgumentException("The argument must be an enumerable"); | |
} | |
T[] ret = new T[arrayInitial.Length + arrayToAppend.Length]; | |
arrayInitial.CopyTo(ret, 0); | |
arrayToAppend.CopyTo(ret, arrayInitial.Length); | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, how about 6 byte[] arrays?
byte[] ID1 = { 0x46, 0x62 };
byte[] ID2= { 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] ID3 = { 0x42, 0x22 };
byte[] queryType = { 0x01, 0x10, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00 };
....
I can not do it at all..