- 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 2628 Articles for Csharp
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
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
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
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
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
488 Views
The nameof operator returns a string literal of an element that can be a variable, type or member.For example, the following is our variable −var vehicle = "motorbike";To get the string literal, use nameof −nameof(vehicle);The following is the code to implement nameof keyword −Example Live Demousing System; public class Program { static void Main() { var vehicle = "motorbike"; Console.WriteLine(nameof(vehicle)); var time = DateTime.Now.ToLocalTime(); Console.WriteLine(nameof(time)); var a = false; Console.WriteLine(nameof(a)); } }Outputvehicle time a
2K+ Views
Use the default operator to get the default value of bool type −bool a = default(bool);Above, we have used the default keyword to get the default value.Let us see the code to display default value of bool −Example Live Demousing System; public class Demo { public static void Main() { bool a = default(bool); // default for bool Console.WriteLine("Default for bool type = "+a); } }OutputThe following is the output. It shows a blank space i.e. False.Default for bool type = False
427 Views
Use the default operator to get the default value of StringBuilder.StringBuilder str = default(StringBuilder);Above, we have used the default keywords to get the default value.Let us see the complete code −Example Live Demousing System; using System.Text; public class Demo { public static void Main() { StringBuilder str = default(StringBuilder); Console.WriteLine("Default for StringBuilder = "+str); } }OutputDefault for StringBuilder =The following is the output. It shows a blank space i.e. Null.Default for StringBuilder = Null
517 Views
Using the default, you can get the default value of every reference and value type. The expressions set as default are evaluated at compile-time.To get the default for int −default(int);To get the default for long −default(long)Let us see the code to display default values −Example Live Demousing System; public class Demo { public static void Main() { int val1 = default(int); long val2 = default(long); bool val3 = default(bool); // default for int Console.WriteLine(val1); // default for long Console.WriteLine(val2); // default for bol Console.WriteLine(val3); } }Output0 0 False
7K+ Views
In a bool array, you can store true and false values. To set a bool array, use the new operator −bool[] arr = new bool[5];To add elements in the array −arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true;Let us see the complete code −Example Live Demousing System; public class Demo { public static void Main() { bool[] arr = new bool[5]; arr[0] = true; arr[1] = true; arr[2] = false; arr[3] = true; arr[4] = true; Console.WriteLine("Displaying values..."); foreach (bool res in arr) { Console.WriteLine(res); } } }OutputDisplaying values... True True False True True