Found 34486 Articles for Programming

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

704 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

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

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

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

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

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

C# Program to perform Currency Conversion

Chandu yadav
Updated on 22-Jun-2020 09:19:32

3K+ Views

Let’s say you need to get the value of 10 dollars in INR.Firstly, set the variables: double usd, inr, val;Now set the dollars and convert it to INR.// how many dpllars usd = 10; // current value of US$ val = 69; inr = usd * val;Let us see the complete code −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          Double usd, inr, val;          // how many dpllars          usd = 10;          // current value of US$          val = 69;          inr = usd * val;          Console.WriteLine("{0} Dollar = {1} INR", usd, inr);       }    } }Output10 Dollar = 690 INR

C# program to print all distinct elements of a given integer array in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:22:53

386 Views

We have set an array and a dictionary to get the distinct elements.int[] arr = {    88,    23,    56,    96,    43 }; var d = new Dictionary < int, int > ();Dictionary collection allows us to get the key and value of a list.The following is the code to display distinct elements of a given integer array −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             88,   ... Read More

Advertisements