
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2587 Articles for Csharp

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

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

768 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

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

799 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

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

671 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

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

218 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); } } }

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