csharp C#:在.NET中合并,追加,扩展两个数组(csharp,mono)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#:在.NET中合并,追加,扩展两个数组(csharp,mono)相关的知识,希望对你有一定的参考价值。

//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;
        }
    }
//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
//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

以上是关于csharp C#:在.NET中合并,追加,扩展两个数组(csharp,mono)的主要内容,如果未能解决你的问题,请参考以下文章

csharp JSON.NET:JObject扩展方法

csharp .NET Xml序列化扩展

csharp 在MVC项目C#中验证文件扩展名(示例中为.pdf)。

csharp 在C#中扩展字符串。此示例扩展字符串类型以添加​​函数以删除字符串的结束值。

csharp log4net记录器扩展

csharp C#文件分割器代码。第二个文件是拆分器合并应用程序。