Found 33676 Articles for Programming

Date format validation using C# Regex

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

16K+ 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

526 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

1K+ 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

438 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

771 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

1K+ 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

813 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

1K+ 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

673 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

Socket Programming in C#

Samual Sam
Updated on 22-Jun-2020 10:16:10

2K+ Views

The System.Net.Sockets namespace has a managed implementation of the Windows Sockets interface.It has two basic modes − synchronous and asynchronous.Let us see an example to work with System.Net.Sockets.TcpListener class −TcpListener l = new TcpListener(1234); l.Start(); // creating a socket Socket s = l.AcceptSocket(); Stream network = new NetworkStream(s);The following is the Socket useful in communicating on TCP/IP network −Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Above, AddressFamily − It is the standard address families by the Socket class to resolve network addressesSocketType − The type of socketProtocolType − This is the network protocol for communication on the Socket. It ... Read More

Advertisements