Use Remove, RemoveAt, and RemoveRange Methods in C# List Collections

karthikeya Boyini
Updated on 21-Jun-2020 15:45:41

1K+ Views

To implement Remove() and RemoveAt() methods in C#, try the following code −Firstly, set a list.List myList = new List() {    "mammals",    "reptiles",    "amphibians",    "vertebrate" };Now, use Remove() method to remove an element.myList.Remove("reptiles");Now, use RemoveAt() method to remove an element by setting the position.myList.RemoveAt(2);The following is the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       List myList = new List() {          "mammals",          "reptiles",          "amphibians",          "vertebrate"       ... Read More

Difference Between a List and an Array in C#

Arjun Thakur
Updated on 21-Jun-2020 15:44:15

8K+ Views

An array stores a fixed-size sequential collection of elements of the same type, whereas list is a generic collection.To define a List −List

Differences Between Class and Struct in C#

Samual Sam
Updated on 21-Jun-2020 15:36:52

703 Views

ClassClass is a blueprint for a data type. A class definition starts with the keyword class followed by the class name.StructA structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.The following are the differences −Classes are reference types and structs are value typesStructures do not support inheritanceStructures cannot have default constructorWhen you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the ... Read More

Final, Finally and Finalize in C#

George John
Updated on 21-Jun-2020 15:36:34

5K+ Views

finalJava has final keyword, but C# does not have its implementation. For the same implementation, use the sealed keyword.With sealed, you can prevent overriding of a method. 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.FinallyThe finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.FinalizeThe ... Read More

Differences Between a Dictionary and an Array in C#

karthikeya Boyini
Updated on 21-Jun-2020 15:36:19

726 Views

DictionaryDictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To declare a Dictionary −IDictionary d = new Dictionary();To add elements −IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);ArrayArray stores a fixed-size sequential collection of elements of the same type. It consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.To define Arrays −int[] arr = new int[5]; To initialize and set elements to Arrays.int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};

Decimal to Multiple Bases Conversion with Stack

karthikeya Boyini
Updated on 21-Jun-2020 15:33:36

684 Views

For multiple-base conversions, set a variable and add the base you want to calculate.Here, for our example, I have set the variable baseNum as 2 −int baseNum = 2;In the same way, if you want base 8, then set the above as −int baseNum = 2; You can also get the above variable value as user input.After getting the value, set a stack and get the values −Stack s = new Stack(); do { s.Push(n % baseNum); n /= baseNum; } while (n != 0);After using the stack, pop out the elements. That would give you the result.Let’s say the ... Read More

Chash Numeric Promotion

Arjun Thakur
Updated on 21-Jun-2020 15:33:17

401 Views

Numeric promotion as the name suggests is the promotion of smaller types to larger types like short to int.In the below example, we have seen numeric promotion in Arithmetic Operator multiply.The short types are automatically promoted to larger types −Exampleusing System; class Program {    static void Main() {       short val1 = 99;       ushort val2 = 11;       int res = val1 * val2;       Console.WriteLine(res);    } }

Chash Numeric Promotion for Conditional Expression

Samual Sam
Updated on 21-Jun-2020 15:30:41

166 Views

Numeric promotion is the promotion of smaller types to larger types like short to int.In the below example, we have seen a numeric promotion in Conditional Expression.The short types are automatically promoted to larger type int.Exampleusing System; class Program {    static void Main() {       short val1 = 99;       int val2;       val2 = (val1 == 1) ? 100 : 30;       Console.WriteLine(val2);    } }OutputAbove, we have used a conditional expression that automatically promoted to int −val2 = (val1 == 1) ? 100 : 30;Here, val2 is an int and val is a short.

Generics vs Non-Generics in C#

Chandu yadav
Updated on 21-Jun-2020 15:28:39

6K+ Views

There are two types of collections in C#: non-generic collections and generic collections.Generics in C#Generic collections hold elements of same datatypes.For example −ListDictionaryHashsetDictionary − Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.Hashset − HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.Non-Generics in C#Non-generic collections hold elements of different datatypes.The following are the non-generic collections: ArrayList, BitArray.ArrayList − It represents ordered collection of an object that can be indexed individually. ArrayList is an alternative to an array. However, unlike array you ... Read More

Use of Internal Keyword in C#

karthikeya Boyini
Updated on 21-Jun-2020 15:28:22

3K+ Views

Internal keyword allows you to set internal access specifier.Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly.Any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined.Exampleusing System; namespace RectangleApplication {    class Rectangle {       internal double length;       internal double width;       double GetArea() {          return length * width;       }       public ... Read More

Advertisements