ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to copy the entire BitArray to a compatible one-dimensional Array (BitArray.CopyTo() Method)

C# BitArray.CopyTo() Method: Here, we are going to learn how to copy the entire BitArray to a compatible one-dimensional Array in C#.Net?
Submitted by Nidhi, on May 04, 2021

The CopyTo() method of BitArray class is used to copy BitArray elements to a one-dimensional array of Boolean values, starting at the specified index of an array.

Syntax:

    void BitArray.CopyTo(Array array, int arrayIndex);

Parameter(s):

  • array: An array to copy elements of BitArray.
  • index: The index in array at which copying begins.

Return value:

It does not return any value.

Exception(s):

  • System.ArgumentNullException
  • System.ArgumentOutOfRangeException
  • System.ArgumentException
  • System.InvalidCastException

Program:

The source code to copy the entire BitArray to a compatible one-dimensional Array is given below. The given program is compiled and executed successfully.

using System;
using System.Collections;

class BitArrayEx
{
    //Entry point of Program
    static public void Main()
    {
        //Creation of BitArray objects
        BitArray bitArr1 = new BitArray(5);
        bool[]  Arr = new bool[8];
        int index = 0;

        bitArr1[0] = true;
        bitArr1[1] = false;
        bitArr1[2] = true;
        bitArr1[3] = false;
        bitArr1[4] = true;

        bitArr1.CopyTo(Arr, 0);

        Console.WriteLine("Elements of BitArray1:");
        for (index = 0; index < bitArr1.Length; index++)
        {
            Console.WriteLine("\tIndex " + index + ": " + bitArr1.Get(index));
        }

        Console.WriteLine("Array Values:");
        for (index = 0; index < Arr.Length; index++)
        {
            Console.WriteLine("\t"+Arr[index]);
        }
    }
}

Output:

Elements of BitArray1:
        Index 0: True 
        Index 1: False
        Index 2: True
        Index 3: False
        Index 4: True
Array Values:
        True
        False
        True
        False
        True
        False
        False
        False
Press any key to continue . . .

C# BitArray Class Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.