Set a two-dimensional array and a one-dimensional array −int[, ] a = new int[2, 2] {{1, 2}, {3, 4} }; int[] b = new int[4];To convert 2D to 1D array, set the two dimensional into one-dimensional we declared before −for (i = 0; i < 2; i++) { for (j = 0; j < 2; j++) { b[k++] = a[i, j]; } }The following is the complete code to convert a two-dimensional array to one-dimensional array in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program { class twodmatrix { ... Read More
The Array.Copy() method in C# is used to copy section of one array to another array.The following is the syntax −Array.Copy(src, dest, length);Here,src = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C# −Exampleusing System; class Program { static void Main() { int[] arrSource = new int[4]; arrSource[0] = 24; arrSource[1] = 33; arrSource[2] = 9; arrSource[3] = 45; int[] arrTarget = new int[3]; Array.Copy(arrSource, arrTarget, 3); Console.WriteLine("Destination Array ..."); foreach (int value in arrTarget) { Console.WriteLine(value); } } }
OverridingUnder overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.Let us see an example of abstract classes that implements Overriding −Exampleusing System; namespace PolymorphismApplication { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a = 0, int b = 0) { length = a; ... Read More
Use the Array.Count property in C# to count the number of items in a list in C# −Set the listList myList = new List() { "electronics", "clothing", "appliances", "accessories" };Now count the number of items in a list in C# −myList.CountThe following is the complete code to count the number of items in a list −Exampleusing System; using System.Collections.Generic; class Program { static void Main() { List myList = new List() { "electronics", "clothing", "appliances", "accessories" }; Console.WriteLine(myList.Count); } }
The Main() method is the entry point −static void Main(string[] args)The arguments array args is used to set arguments −string[] args)It will set the following if you add two arguments −var args = new string[] {"arg1", "arg2”}Here is the demo code −Exampleusing System; namespace Demo { class HelloWorld { // args for command line static void Main(string[] args) { Console.WriteLine("Welcome here!"); Console.ReadKey(); } } }To compile a C# program by using the command-line instead of the Visual Studio IDE ... Read More
Parameters are passed in C# either by value or by reference. With that, you can also use out parameters and param array to pass parameters −ValueThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.ReferenceThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.OutA return statement can be used for returning only one value from a function. However, using output ... Read More
The Array.Copy() method in C# is used to copy section of one array to another array.The following is the syntax −Array.Copy(src, dest, length);Here,src = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C# −Exampleusing System; class Program { static void Main() { int[] arrSource = new int[4]; arrSource[0] = 1; arrSource[1] = 2; arrSource[2] = 3; arrSource[3] = 4; int[] arrTarget = new int[2]; Array.Copy(arrSource, arrTarget, 2); Console.WriteLine("Destination Array ..."); foreach (int value in arrTarget) { Console.WriteLine(value); } } }
Firstly, set the two arrays to be compared −// two arrays int[] arr = new int[] { 99, 87, 56, 45}; int[] brr = new int[] { 99, 87, 56, 45 };Now, use SequenceEqual() to compare the two arrays −arr.SequenceEqual(brr);The following is the code to compare two arrays −Exampleusing System; using System.Linq; namespace Demo { class Program { static void Main(string[] args) { // two arrays int[] arr = new int[] { 99, 87, 56, 45}; int[] brr = new int[] { 99, 87, 56, 45 }; // compare Console.WriteLine(arr.SequenceEqual(brr)); } } }
To compare dates in C#, you need to first set two dates to be compared using the DateTime object. We will use the DateTime class in C# −Date 1DateTime date1 = new DateTime(2018, 08, 05); Console.WriteLine("Date 1 : {0}", date1);Date 2DateTime date2 = new DateTime(2018, 08, 07); Console.WriteLine("Date 2 : {0}", date2);Now let us compare both the dates in C#. The following is an example to compare dates in C# −Exampleusing System; namespace Program { class Demo { static int Main() { DateTime date1 = new DateTime(2018, 08, 05); ... Read More
To compare two dictionaries, firstly set the two dictionaries −Dictionary OneIDictionary d = new Dictionary(); d.Add(1, 97); d.Add(2, 89); d.Add(3, 77); d.Add(4, 88); // Dictionary One elements Console.WriteLine("Dictionary One elements: "+d.Count);Dictionary OneIDictionary d2 = new Dictionary(); d2.Add(1, 97); d2.Add(2, 89); d2.Add(3, 77); d2.Add(4, 88); // Dictionary Two elements Console.WriteLine("Dictionary Two elements: "+d2.Count);Now let us compare them −bool equal = false; if (d.Count == d2.Count) { // Require equal count. equal = true; foreach (var pair in d) { int value; if (d2.TryGetValue(pair.Key, out value)) { if ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP