Declare a list and add elements.List list = new List(); list.Add(50); list.Add(90); list.Add(50); list.Add(100);Now, use Distinct() method to get the unique elements only.List myList = list.Distinct().ToList();The following is the complete code to remove duplicate elements from a List −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(50); list.Add(90); list.Add(50); list.Add(100); Console.WriteLine("Initial List..."); foreach(int a in list) ... Read More
Firstly, declare a HashSet and add elements −var names = new HashSet(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin");To remove an element, use RemoveWhere.names.RemoveWhere(x => x == "John");Let us see the complete example −Exampleusing System; using System.Collections.Generic; public class Program { public static void Main() { var names = new HashSet < string > (); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin"); Console.WriteLine("Initial Set..."); foreach(var val in names) { Console.WriteLine(val); } names.RemoveWhere(x => x ... Read More
Set the list.List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32);To get unique elements.List myList = list.Distinct().ToList();Here is the complete example to display unique values from a list.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo { public static void Main() { List < int > list = new List < int > (); list.Add(55); list.Add(45); list.Add(55); list.Add(65); list.Add(73); list.Add(24); list.Add(65); Console.WriteLine("Initial List..."); ... Read More
Set a string with duplicate words.string str = "One Two Three One";Above, you can see the word “One” comes twice.To remove dulicate words, you can try to run the following code in C# −Example Live Demousing System; using System.Linq; public class Program { public static void Main() { string str = "One Two Three One"; string[] arr = str.Split(' '); Console.WriteLine(str); var a = from k in arr orderby k select k; Console.WriteLine("After removing duplicate words..."); ... Read More
Using Synchronization, you can synchronize access to resources in multithreaded applications.A mutex can be used to synchronize threads across processes. Use it to prevent the simultaneous execution of a block of code by more than one thread at a time.C# lock statement is used to ensure that a block of code runs without interruption by other threads. A Mutual-exclusion lock is obtained for a given object for the duration of the code block.Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to ... Read More
All these keywords are part of the main method of any C# program.The Main method, which is the entry point for all C# programs states that what a class does when it executed.using System; class Demo { static void Main(string[] args) { Console.WriteLine("My first program in C#!"); } }public − This is the access specifier that states that the method can be accesses publically.static − Here, the object is not required to access static members.void − This states that the method doesn’t return any value.main − is As stated above, it s the entry point of a C# ... Read More
The Mutex class in C# is a synchronization primitive that can also be used for interprocess synchronization.Let us see how to create a new Mutex.private static Mutex m = new Mutex();Let us now see how to initialize a new instance of the Mutex class with a Boolean value.private static Mutex m = new Mutex(true);Now let us see how to initialize a new instance of the Mutex class with a Boolean value and the name of the Mutex.Example Live Demousing System; using System.Threading; public class Demo { public static void Main() { Mutex mt = new Mutex(false, ... Read More
In VB, a module is used to store loose code accessible from elsewhere in the application without having to first initialize something.The state of the variable can be easily set or changed and that continues to carry on that value throughout.For the same work in C#< use a static class.Let us see an example −VBModule MyModule Public Sub Display MsgBox("Demo!") End Sub End ModuleC#public static class Display { public static void DisplayMethod() { Console.WriteLine("Demo!"); } }
The .NET Framework 4 brought the System.Collections.Concurrent namespace. This has several collection classes that are thread-safe and scalable. These collections are called concurrent collections because they can be accessed by multiple threads at a time.The following are the concurrent collection in C# −Sr.NoType & Description1BlockingCollectionBounding and blocking functionality for any type.2ConcurrentDictionaryThread-safe implementation of a dictionary of key-value pairs.3ConcurrentQueueThread-safe implementation of a FIFO (first-in, first-out) queue.4ConcurrentStackThread-safe implementation of a LIFO (last-in, first-out) stack.5ConcurrentBagThread-safe implementation of an unordered collection of elements.6IProducerConsumerCollectionThe interface that a type must implement to be used in a BlockingCollectionLet us see how to work with ConcurrentStack that is ... Read More
Firstly, set a string array.string[] values = { "tim", "amit", "tom", "jack", "saurav"};Use the Sort() method to sort.Array.Sort(values);Let us see the complete code −Example Live Demousing System; public class Program { public static void Main() { string[] values = { "tim", "amit", "tom", "jack", "saurav"}; foreach (string value in values) { Console.Write(value); Console.Write(' '); } // sorting Array.Sort(values); Console.WriteLine("Sorted..."); foreach (string value in values) { Console.Write(value); Console.Write(' '); } Console.WriteLine(); } }Outputtim amit tom jack saurav Sorted... amit jack saurav tim tom
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP