Found 2628 Articles for Csharp

What is the difference between public, static and void keywords in C#?

Chandu yadav
Updated on 22-Jun-2020 09:43:18

12K+ Views

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

What is the Mutex class in C#?

Samual Sam
Updated on 22-Jun-2020 09:43:02

1K+ Views

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

Synchronization and Pooling of processes in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:43:32

266 Views

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

CopyOnWriteArrayList version in C#

Arjun Thakur
Updated on 22-Jun-2020 09:27:52

137 Views

Java has CopyOnWriteArrayList, but C# does not have it. For that, the SynchronizedCollection Class in C#, should be preferred.The SyncronizedCollection has a thread-safe collection containing objects of a type. Here is the syntax.public class SynchronizedCollection : IList, ICollection, IEnumerable, IEnumerable, IList, ICollectionAbove, T is the type of object.The following are the properties of the SyncronizedCollection class in C# −Sr.No.Property Name & Description1CountCounts the number of elements in the thread-safe collection.2Item[Int32]Gets an element from the thread-safe collection with a specified index.3ItemsGets the list of elements contained in the thread-safe collection.4SyncRootGets the object used to synchronize access to the thread-safe collection.Read More

What is abstraction in C#?

Samual Sam
Updated on 22-Jun-2020 09:29:18

2K+ Views

Abstraction and encapsulation are related features in object-oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.Abstraction can be achieved using abstract classes in C#. C# allows you to create abstract classes that are used to provide a partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.The following are some of the key points −You cannot create an instance of an abstract classYou cannot ... Read More

String Template Class in C#

Ankith Reddy
Updated on 22-Jun-2020 09:29:37

352 Views

StringTemplate class is used to parse the format string, so that it is compatible with String.Format. The StringTemplate class comes under the NString library that has extension methods. These methods makes string manipulations easy to use like.IsNullOrEmpty() IsNullOrWhiteSpace() Join() Truncate() Left() Right() Capitalize()StringTemplate.Format is better than String.Format since it is more readable and less prone to errors.The order of the values can be easily formatted. The values are formatted in a way similar to String.Format, but with named placeholders instead of numbered placeholders.The following is a sample −string str = StringTemplate.Format("{ExamName} will held on {ExamDate:D}", new { p.ExamName, p.ExamDate }); ... Read More

C# Program to find number of occurrence of a character in a String

karthikeya Boyini
Updated on 22-Jun-2020 09:30:26

700 Views

Let’s say our string is −String s = "mynameistomhanks";Now create a new array and pass it a new method with the string declared above. This calculates the occurrence of characters in a string.static void calculate(String s, int[] cal) {    for (int i = 0; i < s.Length; i++)    cal[s[i]]++; }Let us see the complete code.Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    }    public static void Main() { ... Read More

What is an object pool in C#?

George John
Updated on 22-Jun-2020 09:30:43

1K+ Views

Object pool is a software construct designed to optimize the usage of limited resources. It has objects that are ready to be used.The pooled objects can be reused. The object pooling has two forms −On activation of the object, it is pulled from pool.On deactivation, the object is added to the pool.Configure object pooling by applying the ObjectPoolingAttribute attribute.This is applied to a class deriving from the System.EnterpriseServices.ServicedComponent class.To understand how a pool behaves, the Diagnostics class has informational properties. Through this, you can check the behavior under dissimilar scenarios.The usage of Object pool can be understood when a part ... Read More

C# program to find common elements in three arrays using sets

Chandu yadav
Updated on 22-Jun-2020 09:34:05

390 Views

Set three arraysint[] arr1 = {    99,    57,    63,    98 }; int[] arr2 = {    43,    99,    33,    57 }; int[] arr3 = {    99,    57,    42 };Now set the above elements using HashSet.// HashSet One var h1 = new HashSet < int > (arr1); // HashSet Two var h2 = new HashSet < int > (arr2); // HashSet Three var h3 = new HashSet < int > (arr3);Let us see the complete code to find common elements.Exampleusing System; using System.Collections.Generic; using System.Linq; public class Program ... Read More

C# program to find Largest, Smallest, Second Largest, Second Smallest in a List

Samual Sam
Updated on 22-Jun-2020 09:31:34

2K+ Views

Set the listvar val = new int[] {    99,    35,    26,    87 };Now get the largest number.val.Max(z => z);Smallest numberval.Min(z => z);Second largest numberval.OrderByDescending(z => z).Skip(1).First();Second smallest numberval.OrderBy(z => z).Skip(1).First();The following is the code −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       var val = new int[] {          99,          35,          26,          87       };       var maxNum = val.Max(z => z);       ... Read More

Advertisements