Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 53 of 151

What are the differences between a multi-dimensional array and jagged array?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 1K+ Views

Multi-dimensional arrayMulti-dimensional arrays are also called rectangular array. You can define a 3-dimensional array of integer as −int [ , , ] val;Let us see how to define a two-dimensional array.int[,] val = new[3,3] Jagged arrayA Jagged array is an array of arrays. To access an element from it, just mention the index for that particular array.Here, we have a jagged array with 5 array of integers −int[][] a = new int[][]{new int[]{0,0},new int[]{1,2}, new int[]{2,4},new int[]{ 3, 6 }};

Read More

How to copy or clone a C# list?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 3K+ Views

To copy or clone a C# list, firstly set a list −List < string > list1 = new List < string > (); list1.Add("One"); list1.Add("Two"); list1.Add("Three"); list1.Add("Four");Now declare a string array and use the CopyTo() method to copy.string[] arr = new string[20]; list1.CopyTo(arr);Let us see the complete code to copy a list into a one-dimensional array.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < string > list1 = new List < string > ();       list1.Add("One");       list1.Add("Two");       list1.Add("Three");   ...

Read More

What is the difference between implicit and explicit type conversion in C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 1K+ Views

The following is the difference between implicit and explicit type conversion −Implicit Type ConversionThese conversions are performed by C# in a type-safe manner.To understand the concept, let us implicitly convert int to long.int val1 = 11000; int val2 = 35600; long sum; sum = val1 + val2;Above, we have two integer variable and when we sum it in a long variable, it won’t show an error. Since the compiler does the implicit conversion on its own.Let us print the values now.Exampleusing System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) ...

Read More

How to add integer values to a C# list?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 9K+ Views

To add integer values to a list in C#, use the Add() method.Firstly, declare an integer list in C# −List list1 = new List();Now add integer values −list1.Add(900); list1.Add(400); list1.Add(300);Let us see the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       public static void Main(String[] args) {          List list1 = new List();          list1.Add(900);          list1.Add(400);          list1.Add(300);          Console.WriteLine(list1.Count);       }    } }

Read More

How to add read-only property in C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 349 Views

A field marked "read-only", can only be set once during the construction of an object. It cannot be changed −Let us see an example.class Employee {    readonly int salary;    Employee(int salary) {       this.salary = salary;    }    void UpdateSalary() {       //salary = 50000; // Compile error    } }Above, we have set the salary field as read-only.If you will change it, then a compile-time error will occur. The same is shown in the above example.Let us now see how to check whether an array is read-only or not −Exampleusing ...

Read More

How to create a Directory using C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 469 Views

To create, move and delete directories in C#, the System.IO.Directory class has methods.Firstly, import the System.IO namespace.Now, use the Director.CreateDirectory() method to create a directory in the specified path −string myDir = @"D:\NEW"; if (!Directory.Exists(myDir)) {    Directory.CreateDirectory(myDir); }In the same way, you can create a sub-directory −string mysubdir = @"C:\NEW\my\"; Directory.CreateDirectory(mysubdir);

Read More

How to compare two lists for equality in C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 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

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

Samual Sam
Samual Sam
Updated on 21-Jun-2020 762 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 compare two arrays in C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 1K+ Views

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

Read More

How to convert Trigonometric Angles in Radians using C#?

Samual Sam
Samual Sam
Updated on 21-Jun-2020 3K+ Views

To convert trigonometric angles in Radians, multiply by Math.PI/180. This will convert degrees to radians.The following is the code −Exampleusing System; class Program {    static void Main() {       Console.WriteLine(Math.Cos(45));       double res = Math.Cos(Math.PI * 45 / 180.0);       Console.WriteLine(res);    } }Above, we converted using the following formulae −Math.PI * angle / 180.0

Read More
Showing 521–530 of 1,507 articles
« Prev 1 51 52 53 54 55 151 Next »
Advertisements