Ankith Reddy

Ankith Reddy

730 Articles Published

Articles by Ankith Reddy

Page 27 of 73

Initializing HashSet in C#

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 1K+ Views

To initialize a HashSet.var h = new HashSet(arr1);Above, we have set an array in the HashSet. The following is the array −string[] arr1 = {    "electronics",    "accessories”,    "electronics", };The following is an example showing how to implement HashSet in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "electronics",          "accessories”,          "electronics",       };       Console.WriteLine(string.Join(",", arr1));       // HashSet       var h = new HashSet(arr1);       // eliminates duplicate words       string[] arr2 = h.ToArray();       Console.WriteLine(string.Join(",", arr2));    } }

Read More

What is Cast Operator () in C#?

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 843 Views

Type conversion is converting one type of data to another type. Explicit conversions are done explicitly by users using the pre-defined functions and require a cast operator.Let us see an example to cast double to int −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          double a = 4563.56;          int x;          x = (int)a;          Console.WriteLine(x);          Console.ReadKey();       }    } }To cast double to int, we perfomed explicit type casting −x = (int)a;

Read More

How to add items/elements to an existing jagged array in C#?

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 1K+ Views

To add an element to existing jagged array, just set the value of the element with a new value.Let’s say you need to add an element at the following location −a[3][1]Just set the value −a[3][1] = 500;Above, we accessed the first element of the 3rd array in a jagged array.Let us see the complete code −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[][] x = new int[][]{new int[]{10, 20}, new int[]{30, 40}, new int[]{50, 60}, new int[]{ 70, 80 }, new int[]{ 90, 100 ...

Read More

Value Type vs Reference Type in C#

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 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 compare two lists and add the difference to a third list in C#?

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 2K+ 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 access elements from an array in C#?

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 950 Views

First, define and initalize an array −int[] p = new int[3] {99, 92, 95};Now, display the array elements −for (j = 0; j < 3; j++ ) {    Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]); }To acess any element, just include the index of the element you want like this −p[2];The above is to acess the 3rd element.Now let us see the complete code −Exampleusing System; namespace Program {    class Demo {       static void Main(string[] args) {          int[] p = new int[3] {99, 92, 95};          int j;          for (j = 0; j < 3; j++ ) {             Console.WriteLine("Price of Product[{0}] = {1}", j, p[j]);          }          // access          int e = p[2];          Console.WriteLine("Product 3rd price: "+e);          Console.ReadKey();       }    } }

Read More

C# object serialization

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 298 Views

For object serialization, you need to refer the below code. Here, we have use the BinaryFormatter.Serialize (stream, reference) method to serialize our sample object.We have set a constructor here −public Employee(int id, string name, int salary) {    this.id = id;    this.name = name;    this.salary = salary; }Now set the file stream −FileStream fStream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter bFormat = new BinaryFormatter();An object of the Employee class −Employee emp = new Employee(001, "Jim", 30000); bFormat.Serialize(fStream, emp);

Read More

What are sealed modifiers in C#?

Ankith Reddy
Ankith Reddy
Updated on 21-Jun-2020 526 Views

When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.Let us see an example −The following example won’t allow you to override the method display() because it has a sealed modifier for the ClassTwo derived class −ClassOne is our base class, whereas ClassTwo and ClassThree are derived classes −Exampleclass ClassOne {    public virtual void display() {       Console.WriteLine("baseclass");    } } class ClassTwo : ClassOne {    public sealed override ...

Read More

What are finalizers in C#?

Ankith Reddy
Ankith Reddy
Updated on 20-Jun-2020 1K+ Views

Finalizers in C# are used to destruct instances of classes. With that, you can also use it to release resources.Here are some of the key points about Finalizers −Only one finalizer is allowed for a classYou cannot inherit or overload FinalizersA finalizer cannot have parametersFinalizers invoke automaticallyFinalizers in C# are declared like destructors. Let’s say the class name is Demo, therefore, the following would be our finalizer −~Demo() {    // }The finalizer declaration is prefixed with a tilde before the class name.

Read More

What are nested namespaces in C#?

Ankith Reddy
Ankith Reddy
Updated on 20-Jun-2020 2K+ Views

A namespace inside a namespace is called a nested namespace in C#. This is mainly done to properly structure your code.We have an outer namespace −namespace outer {}Within that, we have an inner namespace inside the outer namespace −namespace inner {    public class innerClass {       public void display() {          Console.WriteLine("Inner Namespace");       }    } }Now to call the method of inner namespace, set a class object of the inner class and call the method as shown in the below example −namespace outer {    class Program {   ...

Read More
Showing 261–270 of 730 articles
« Prev 1 25 26 27 28 29 73 Next »
Advertisements