Karthikeya Boyini has Published 2193 Articles

How to iterate over a C# dictionary?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 12:11:55

484 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 −Example Live Demousing System; using System.Collections.Generic; public class Demo ... Read More

How to list down all the files available in a directory using C#?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 12:09:50

399 Views

Firstly, use the DirectoryInfo object −//creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");Now, use the GetFiles() method to get all the files −FileInfo [] f = mydir.GetFiles();To get the list of files in a directory, try to run the following code −Exampleusing System; using System.IO; namespace Demo { ... Read More

C# Program to check if a number is Positive, Negative, Odd, Even, Zero

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 12:08:17

2K+ Views

Check for the following conditions −For odd and even, check for the remainder when the number is divided by 2 −// checking for odd/ even if(n % 2 == 0) {    Console.WriteLine("Even"); } else {    Console.WriteLine("Odd"); }For positive, negative and checking whether a number is 0 or not ... Read More

How can we check the current MySQL transaction isolation level?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 11:43:36

3K+ Views

By executing SELECT @@TX_ISOLATION command we can check the current MySQL transaction isolation level.Examplemysql> SELECT @@TX_ISOLATION; +-----------------+ | @@TX_ISOLATION  | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec)

Flow control in try catch finally in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:58:29

706 Views

The flow control in try, catch, and finally can be understood using the following example. Here, we are dividing two numbers −Example Live Demousing System; namespace ErrorHandlingApplication {    class DivNumbers {       int result;       DivNumbers() {          result = 0;   ... Read More

How to write "Hello World" in C#?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:56:46

567 Views

To print “Hello World” in C#, use the Console.WriteLine.Let us see a basic C# program to display a text −Example Live Demousing System; using System.Collections.Generic; using System.Text; namespace Program {    class MyApplication {       static void Main(string[] args) {          Console.WriteLine("Hello World");     ... Read More

Sort an array in descending order using C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:25:12

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 ... Read More

Date format validation using C# Regex

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:23:28

16K+ Views

Use the DateTime.TryParseExact method in C# for Date Format validation.They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.Example Live Demousing System; using System.Globalization; namespace Demo {    class Program {     ... Read More

C# program to merge two sorted arrays into one

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:22:04

2K+ Views

Set two arrays that you wish to merge −int[] arr1 = new int[5] {    5,    15,    25,    30,    47 }; int[] arr2 = new int[5] {    55,    60,    76,    83,    95 };Now take a third array that would merge both ... Read More

What is unboxing in C#?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 10:19:54

218 Views

Boxing is implicit and unboxing is explicit. Unboxing is the explicit conversion of the reference type created by boxing, back to a value type.Let us see an example of variable and object in C# −// int int x = 30; // Boxing object obj = x; // Un ... Read More

Advertisements