karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 55 of 143

C# program to count the occurrences of each character

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 33K+ Views

Firstly, set the string −string str = "Website"; Console.WriteLine("String: "+str);Check for every character in the string and increment a variable that would count the number of occurrences of that character −for (int j = 0; j < str.Length; j++) {    if (str[0] == str[j]) {       cal++;    } }ExampleYou can try to run the following code to count the occurrences of each character.using System; public class Demo {    public static void Main() {       string str = "Website";       Console.WriteLine("String: "+str);       while (str.Length > 0) {     ...

Read More

C# Program to find the largest element from an array

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

Declare an array −int[] arr = { 20, 50, -35, 25, 60 };Now to get the largest element from an array, use the Max() method −arr.Max());Here is the complete code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 20, 50, -35, 25, 60 };       Console.WriteLine(arr.Max());    } }Output60

Read More

C# program to count total set bits in a number

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 390 Views

The number for our example is 11 i.e. binary −1101The total set bits are 3 in 1101; to find it, use a loop till it’s not equal to 0. Here, our num is 11 i.e. decimal −while (num>0) {    cal += num & 1;    num >>= 1; }ExampleTo count total set bits in a number, use the following code.using System; public class Demo {    public static void Main() {       int cal = 0;       // Binary is 1011       int num = 11;       while (num>0) {          cal += num & 1;          num >>= 1;       }       // 1 bits in 1101 are 3       Console.WriteLine("Total bits: "+cal);    } }OutputTotal bits: 3

Read More

What are the differences between ref and out parameters in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 675 Views

Ref ParameterA reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters.You can declare the reference parameters using the ref keyword. The following is an example −Exampleusing System; namespace CalculatorApplication {    class NumberManipulator {       public void swap(ref int x, ref int y) {          int temp;          temp = x; /* save the value of x */          x = y; /* put y ...

Read More

Group by Operator in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 208 Views

Use the group by the operator in C# to separate the results of an expression into parts.Let’s say the following is our array −int[] a = { 5, 10, 15, 20, 25, 30 };Now, using Group by and orderby, we will find the elements greater than 20 −var check = from element in a orderby element group element by chkGreater(element);The following is the complete code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       int[] a = { 5, 10, 15, 20, 25, 30 };       var check = from element in ...

Read More

How to iterate any Map in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

C# has no built-in Math type. For the same, use a Dictionary.Firstly, create a Dictionary −Dictionary d = new Dictionary(); d.Add("keyboard", 1); d.Add("mouse", 2);Get the keys −var val = d.Keys.ToList();Now, use the foreach loop to iterate over the Map −foreach (var key in val) {    Console.WriteLine(key); }To iterate it, try to run the following code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       Dictionary d = new Dictionary();       d.Add("keyboard", 1);       d.Add("mouse", 2);       // get keys     ...

Read More

Set warning action for a list item in a list group with Bootstrap

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 153 Views

To set warning action for a list item in a list group, use the .list-group-item-warning classExample           Bootstrap Example                                          Demo                                      A                                        B                                        C                                        D                                

Read More

C# Program to display name of Current Thread

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 292 Views

To display the name of the current thread in C#, use the Name property.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.Name property to display name of the thread −thread.NameExampleLet us see the complete code show current thread’s name in C#.using System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My Thread";          Console.WriteLine("Thread Name = {0}", thread.Name);          Console.ReadKey();       }    } }OutputThread Name = My Thread

Read More

How to iterate over a C# dictionary?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 530 Views

Firstly, add elements −IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);Now, get the keys −List myList = new List(d.Keys);To iterate −foreach (int k in myList) {    Console.WriteLine("{0}, {1}", k, d[k]); }The following is an example −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary < int, int > d = new Dictionary < int, int > ();       d.Add(1, 97);       d.Add(2, 89);       d.Add(3, 77);       d.Add(4, 88);       List < int > myList = new List < int > (d.Keys);           foreach(int k in myList) {          Console.WriteLine("{0}, {1}", k, d[k]);       }    } }Output1, 97 2, 89 3, 77 4, 88

Read More

Round a number to the nearest even number in C#

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 844 Views

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.Declare and initialize a decimal number −decimal val = 25.55M;To round a number to the nearest even number −decimal.Round(val, 0, MidpointRounding.ToEven)Here is the complete code −Exampleusing System; using System.Linq; class Demo {    static void Main() {       decimal val = 25.55M;       Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven));    } }Output26

Read More
Showing 541–550 of 1,421 articles
« Prev 1 53 54 55 56 57 143 Next »
Advertisements