Server Side Programming Articles - Page 2506 of 2650

Value Type vs Reference Type in C#

Ankith Reddy
Updated on 21-Jun-2020 14:01:19

8K+ Views

Value Type and Reference, both are types in C# −Value TypeValue type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. When you declare an int type, the system allocates memory to store the value.Value Type variables are stored in the stack.Examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively.Reference TypeIt refers to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the ... Read More

How to create a Dictionary using C#?

karthikeya Boyini
Updated on 21-Jun-2020 14:02:04

237 Views

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To create a dictionary, you first need to set it and add the key and values. Here we have added 5 keys with values to a Dictionary. We have set its keys and value type as int.IDictionary d = new Dictionary(); d.Add(1, 44); d.Add(2, 34); d.Add(3, 66); d.Add(4, 47); d.Add(5, 76);The following is the complete code −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary d = new Dictionary();       d.Add(1, 44); ... Read More

How to count the number of items in a C# list?

George John
Updated on 21-Jun-2020 13:35:06

925 Views

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);    } }

What is the difference between function overriding and method hiding in C#?

Samual Sam
Updated on 21-Jun-2020 13:36:43

696 Views

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

How to copy a section of an array into another array in C#?

Chandu yadav
Updated on 21-Jun-2020 13:38:56

304 Views

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);       }    } }

How to convert a 2D array into 1D array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:42:39

3K+ Views

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

How to convert a Decimal to Octal using C#?

Arjun Thakur
Updated on 21-Jun-2020 13:43:30

418 Views

To get the octal equivalent, use a while loop for the decimal value and store the remainder in the array set for octal. Here we have set the remainder by mod 8 in the array.Then divide the number by 8 −while (dec != 0) {    oct[i] = dec % 8;    dec = dec / 8;    i++; }Let us see the complete code.Here, our decimal number is 18 −using System; namespace Demo {    class Program {       static void Main(string[] args) {          int []oct = new int[30];          // decimal          int dec = 18;          int i = 0;          while (dec != 0){             oct[i] = dec % 8;             dec = dec / 8;             i++;          }          for (int j = i - 1; j >= 0; j--)          Console.Write(oct[j]);          Console.ReadKey();       }    } }

How to compare two lists for equality in C#?

Samual Sam
Updated on 21-Jun-2020 13:44:24

2K+ Views

Set the two lists −List OneList < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");List TwoList < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");Now if the following returns different elements, then it would mean the lists are not equal −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("P");       list1.Add("Q");       list1.Add("R");       Console.WriteLine("First list..."); ... Read More

How to compare two lists and add the difference to a third list in C#?

Ankith Reddy
Updated on 21-Jun-2020 13:47:41

1K+ Views

First, set the two lists −List OneList < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D");List TwoList < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D");To find the difference between the two list and display the difference elements −IEnumerable < string > list3; list3 = list1.Except(list2); foreach(string value in list3) {    Console.WriteLine(value); }The following is the complete example to compare two lists −Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new ... Read More

How to compare two dictionaries in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:18:26

3K+ Views

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

Advertisements