Found 2587 Articles for Csharp

CopyOnWriteArrayList version in C#

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

248 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

461 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

965 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

523 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

C# program to find all duplicate elements in an integer array

karthikeya Boyini
Updated on 22-Jun-2020 09:35:59

9K+ Views

Firstly, set the array with duplicate elements.int[] arr = {    24,    10,    56,    32,    10,    43,    88,    32 };Now declare a Dictionary and loop through the array to get the repeated elements.var d = new Dictionary < int, int > (); foreach(var res in arr) {    if (d.ContainsKey(res))          d[res]++;    else    d[res] = 1; }Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {   ... Read More

C# program to convert time from 12 hour to 24 hour format

George John
Updated on 22-Jun-2020 09:36:23

12K+ Views

Firstly, set the 12 hr format date.DateTime d = DateTime.Parse("05:00 PM");Now let us convert it into 24-hr format.d.ToString("HH:mm"));The following is the code to covert time from 12 hour to 24 hour format −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          DateTime d = DateTime.Parse("05:00 PM");          Console.WriteLine(d.ToString("HH:mm"));       }    } }Output17:00

C# program to split the Even and Odd integers into different arrays

Samual Sam
Updated on 22-Jun-2020 09:37:39

2K+ Views

Take two arrays:int[] arr2 = new int[5]; int[] arr3 = new int[5];Now, if the array element gets the remainder 0 on dividing by 2, it is even. Get those elements and add in another array. This loops through the length of the array:if (arr1[i] % 2 == 0) {    arr2[j] = arr1[i]; }In the else condition, you will get the odd elements. Add them to a separate array and display them individually as shown in the below example:Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {     ... Read More

Advertisements