Found 2628 Articles for Csharp

C# program to list the difference between two lists

Samual Sam
Updated on 22-Jun-2020 10:23:00

1K+ Views

To get the difference between two lists, firstly set two lists in C# −// first list List < string > list1 = new List < string > (); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); // second list List < string > list2 = new List < string > (); list2.Add("C"); list2.Add("D"); foreach(string value in list2) {    Console.WriteLine(value); }To get the difference, use IEnumerable and Except() as shown below. The difference is shown in the third list −IEnumerable < string > list3; list3 = list1.Except(list2);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo ... Read More

Date format validation using C# Regex

karthikeya Boyini
Updated on 22-Jun-2020 10:23:28

13K+ Views

Use the DateTime.TryParseExact method in C# for Date Format validation.They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.Example Live Demousing System; using System.Globalization; namespace Demo {    class Program {       static void Main(string[] args) {          DateTime d;          bool chValidity = DateTime.TryParseExact(          "08/14/2018",          "MM/dd/yyyy",          CultureInfo.InvariantCulture,          DateTimeStyles.None,          out d);          Console.WriteLine(chValidity);       }    } }OutputTrue

SortedMap Interface in C#

Samual Sam
Updated on 22-Jun-2020 10:23:59

289 Views

Java has SortedMap Interface, whereas an equivalent of it in C# is SortedList.SortedList collection in C# use a key as well as an index to access the items in a list.A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.Let us see an example to work with ... Read More

Sort the words in lexicographical order in C#

Arjun Thakur
Updated on 22-Jun-2020 10:24:33

785 Views

Firstly, set a string array −string[] arr = new string[] {    "Indian",    "Moroccon",    "American", };To sort the words in lexicographical order −var sort = from a in arr orderby a select a;Example Live DemoLet us see the complete code −using System; using System.Linq; class Program {    static void Main() {       string[] arr = new string[] {          "Indian",          "Moroccon",          "American",       };       var sort = from a in arr       orderby a       select a;       foreach(string res in sort) {          Console.WriteLine(res);       }    } }outputAmerican Indian Moroccon

Sort an array in descending order using C#

karthikeya Boyini
Updated on 22-Jun-2020 10:25:12

335 Views

Declare an array and initialize −int[] arr = new int[] {    87,    23,    65,    29,    67 };To sort, use the Sort() method and CompareTo() to compare and display in decreasing order −Array.Sort < int > (arr, new Comparison < int > ((val1, val2) => val2.CompareTo(val1)));Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Text; public class Demo {    public static void Main(string[] args) {       int[] arr = new int[] {          87,          23,          65,     ... Read More

Use ReaderWriter Lock in C#

George John
Updated on 30-Jul-2019 22:30:23

531 Views

ReaderWriterLock synchronize access to a resource. A ReaderWriterLock has better throughput than a Monitor, which is one-at-a-time lock. This works for a situation when a resource is changed rarely. Let us see how to declare a ReaderWriter lock in C# − static ReaderWriterLock r = new ReaderWriterLock(); The following are the properties of a ReaderWriter lock in C# − Sr.No. Property & Description 1 IsReaderLockHeld Gets a value indicating whether the current thread holds a reader lock. 2 IsWriterLockHeld Gets a value indicating whether the current thread holds a Writer lock. ... Read More

Unit Testing for C# Code

Samual Sam
Updated on 30-Jul-2019 22:30:23

924 Views

Unit testing is a key for C# code since it can help in maintaining code in the development process. It lets you know about the problems in the development cycle. With Unit Testing, you can make the code reliable and reusable. One of the fundamental principles of adopting unit testing is to follow a TDD (Test Driven Development) approach where we have to write tests case first, and then write the simple code that will make the test pass For Unit testing, you need to work with Microsoft Testing tools, which is what we call MS Unit Test. To create ... Read More

Thread-Safe collections in C#

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

658 Views

The .NET Framework 4 introduced System.Collections.Concurrent namespace. The namespace has numerous collection classes. These classes are both thread-safe and scalable. Multiple threads can safely add or remove items from these collections, The following concurrent collection types use lightweight synchronization mechanisms: SpinLock, SpinWait, etc. These are new in .NET Framework 4. Let us see the concurrent collection in C# − Sr.No. Type & Description 1 BlockingCollection Bounding and blocking functionality for any type. 2 ConcurrentDictionary Thread-safe implementation of a dictionary of key-value pairs. 3 ConcurrentQueue Thread-safe implementation of a FIFO ... Read More

Thread-based parallelism in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:15:20

874 Views

In C#, Task parallelism divide tasks. The tasks are then allocated to separate threads for processing. In .NET, you have the following mechanisms to run code in parallel: Thread, ThreadPool, and Task. For parallelism, use tasks in C# instead of Threads.A task will not create its own OS thread, whereas they are executed by a TaskScheduler.Let us see how to create tasks. Use a delegate to start a task −Task tsk = new Task(delegate { PrintMessage(); }); tsk.Start();Use Task Factory to start a task −Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });You can also use Lambda −Task tsk = new Task( () => PrintMessage() ... Read More

Thread Synchronization in C#

Chandu yadav
Updated on 22-Jun-2020 10:15:44

467 Views

Synchronize access to resources in multithreaded applications using Synchronization.Mutex to Synchronize ThreadsA 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.A lock statement gets an object as an argument. The parameter given to the “lock” should be an object based on a reference type −public class Demo ... Read More

Advertisements