- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34487 Articles for Programming
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
156 Views
Read individual bytes using GetByte() method in C# −Set an array −int[] arr = { 3, 4, 12 };Now, use Buffer.GetByte() to display the array elements and to read individual bytes −for (int i = 0; i < Buffer.ByteLength(arr); i++) { Console.WriteLine(Buffer.GetByte(arr, i)); }The following is the code −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { int[] arr = { 3, 4, 12 }; // loop through the byte array for (int i = 0; i < Buffer.ByteLength(arr); i++) { Console.WriteLine(Buffer.GetByte(arr, i)); } } }Output3 0 0 0 4 0 0 0 12 0 0 0
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
1K+ Views
Set a byte array −byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };To count number of bytes −Buffer.ByteLength(b)The following is the code −Example Live Demousing System; class Program { static void Main() { byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 }; int len = Buffer.ByteLength(b); for (int i = 0; i < len; i++) { Console.WriteLine(b[i]); } Console.WriteLine("Length of byte array = "+len); } }Output5 9 19 23 29 35 55 78 Length of byte array = 8
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
774 Views
To replace a C# aray with a new array, use the Array.Resize.Withing that, set the size of the new array −Array.Resize(ref arr, 4);Now add the new elements to the array as shown below −Example Live Demousing System; class Program { static void Main() { char[] arr = new char[5]; arr[0] = 'J'; arr[1] = 'A'; Array.Resize(ref arr, 4); // Set value for new elements arr[2] = 'C'; arr[3] = 'K'; Console.WriteLine("Updated Array : "+ new string(arr)); } }OutputUpdated Array : JACK
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
519 Views
It copies the bytes from one byte array to another byte array.Example Live Demousing System; class Demo { static void Main() { // byte arrays byte[] b1 = new byte[] {55, 66, 77, 88, 99}; byte[] b2 = new byte[8]; // copying bytes from one to another Buffer.BlockCopy(b1, 0, b2, 0, 5); /* calling the method with the byte array b2 that has the copied elements */ bufferFunc(b2); } static void bufferFunc(byte[] a) { for (int j = 0; j < a.Length; j++) { Console.Write(a[j]); } Console.WriteLine(); } }Output5566778899000
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
480 Views
To handle range of bytes, use Buffer Type in C#. Its method Buffer.BlockCopy copies the bytes from one byte array to another byte array.Example Live Demousing System; class Demo { static void Main() { // byte arrays byte[] b1 = new byte[] {39, 45, 58 }; byte[] b2 = new byte[5]; // copying bytes from one to another Buffer.BlockCopy(b1, 0, b2, 0, 3); /* calling the method with the byte array b2 that has the copied elements */ bufferFunc(b2); } static void bufferFunc(byte[] a) { for (int j = 0; j < a.Length; j++) { Console.Write(a[j]); } Console.WriteLine(); } }Output39455800
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
4K+ Views
You cannot resize an array in C#, but using Array.Resize you can replace the array with a new array of different size.The following is our array −char[] ch = new char[10]; ch[0] = 'a'; ch[1] = 'b';Now, resize −Array.Resize(ref ch, 10);The following is the complete example −Example Live Demousing System; class Program { static void Main() { char[] ch = new char[10]; ch[0] = 'a'; ch[1] = 'b'; // Resize array Array.Resize(ref ch, 10); // Set value for new elements ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
4K+ Views
Firstly, declare and initialize two arrays −int[] arr1 = { 37, 45, 65 }; int[] arr2 = { 70, 89, 118 };Now create a new list −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);Use the AddRange() method the arrays into the newly created list.myList.AddRange(arr1); myList.AddRange(arr2);Now convert the list into array as shown below −Example Live Demousing System; using System.Collections.Generic; class Demo { static void Main() { int[] arr1 = { 37, 45, 65 }; int[] arr2 = { 70, 89, 118 }; // displaying array1 Console.WriteLine("Array 1..."); ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
224 Views
NullReferenceException occurs when you try to to access member fields, or function types that points to null.Here is an example −Example Live Demousing System; class Demo { static void Main() { string str = null; if (str.Length > 0) { Console.WriteLine(str); } } }OutputThe following is the output. It throws NullReferenceException, since you are tryonhg access a memebt that points to null −Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
24 Views
It gets an array of the names of constants in an enumeration. The following is the syntax −Enum.GetNames(Type)Here, Type is an enumeration type.The following is an example −Example Live Demousing System; class Demo { enum Vehicle { Car, Motorbike, Truck, }; static void Main() { // display the enum foreach ( string res in Enum.GetNames ( typeof (Vehicle))) Console.WriteLine (res); } }OutputCar Motorbike Truck
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
533 Views
Gets the string representation of an Enum value using the Enum.GetName.It has two parameters −Type − Enumeration typeObject − Value of an enumerationThe following is an example −Example Live Demousing System; class Demo { enum Vehicle { Car, Motorbike, Truck, Bicycles }; static void Main() { // Usig GetName to get the string representation of enum type string res = Enum.GetName(typeof(Vehicle), Vehicle.Motorbike); // Displaying Console.WriteLine(res); } }OutputMotorbike