Contract and Append to arrays

This is a discussion on Contract and Append to arrays within the C# Programming forums, part of the General Programming Boards category; One of my favorite programming languages, D, has two operators to easily contract or append to arrays (all types of ...

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Contract and Append to arrays

    One of my favorite programming languages, D, has two operators to easily contract or append to arrays (all types of arrays, not just "char[]", as in the example below):

    Code:
    // contract
    char[] array1 = "Hello, ";
    char[] array2 = "world!";
    char[] array3 = array1 ~ array2;
    
    // append
    char[] array4 = "Hey, ";
    array4 ~= array2;
    I want to know if C# has some operator or function that can do either of these.
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    No, there's not.

    Writing a function to do that is pretty easy though...
    Code:
            private object[] combinearrays(object[] array1, object[] array2)
            {
                object[] array3 = new object[array1.Length + array2.Length];
                array1.CopyTo(array3, 0);
                array2.CopyTo(array3, array1.Length);
                return array3;
            }

  3. #3
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Quote Originally Posted by BMJ
    No, there's not.
    Where's the MS suggestion box? lol

    Thx for the code, but it's not working:

    Code:
    private byte[] combinebytes(byte[] array1, byte[] array2)
    {
    	byte[] array3 = new byte[array1.LongLength + array2.LongLength + 1];
    	// gives "Destination array not long enough" error
    	array3.CopyTo(array1, 0);
    	array3.CopyTo(array2, array1.LongLength);
    	return array3;
    }
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21