Found 34488 Articles for Programming

Sort the words in lexicographical order in C#

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

786 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

337 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

926 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

878 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

469 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

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

Singly LinkedList Traversal using C#

George John
Updated on 22-Jun-2020 10:16:46

143 Views

Declare a LinkedList using the LinkedList collection in X# −var a = new LinkedList < string > ();Now add elements to the LinkedList −a.AddLast("Tim"); a.AddLast("Tom");Let us see how to perform traversal in a LinkedList −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var a = new LinkedList < string > ();       a.AddLast("Tim");       a.AddLast("Tom");       foreach(var res in a) {          Console.WriteLine(res);       }    } }

Singleton Class in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:17:21

3K+ Views

Singleton Class allow for single allocations and instances of data. It has normal methods and you can call it using an instance.To prevent multiple instances of the class, the private constructor is used.Let us see an example −public class Singleton {    static Singleton b = null;    private Singleton() {       }   }The following is another example displaying how to display Singleton class −Example Live Demousing System; class Singleton {    public static readonly Singleton _obj = new Singleton();          public void Display() {       Console.WriteLine(true);    }    Singleton() {} } class Demo {    public static void Main() {       Singleton._obj.Display();    } }OutputTrue

Advertisements