Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by karthikeya Boyini
Page 53 of 143
C# Multiple Local Variable Declarations
In C#, you can use the comma to declare more than one local variable in a statement. The following displays the same −int a = 20, b = 70, c = 40, d = 90;ExampleLet us see an example in which we are declaring multiple local variables. Below four variable is declared and initialized in the same statement.using System; class Demo { static void Main() { int a = 20, b = 70, c = 40, d = 90; Console.WriteLine("{0} {1} {2} {3}", a, b, c, d); } }Output20 70 40 90
Read MoreToDictionary method in C#
The ToDictionary method is an extension method in C# and converts a collection into Dictionary.Firstly, create a string array −string[] str = new string[] {"Car", "Bus", "Bicycle"};Now, use the Dictionary method to convert a collection to Dictionary −str.ToDictionary(item => item, item => true);Here is the complete code −Exampleusing System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { string[] str = new string[] {"Car", "Bus", "Bicycle"}; // key and value under ToDictionary var d = str.ToDictionary(item => item, item => true); foreach (var ele in ...
Read MoreC# Program to count the number of lines in a file
Firstly, create a file using StreamWriter class and add content to it −using (StreamWriter sw = new StreamWriter("hello.txt")) { sw.WriteLine("This is demo line 1"); sw.WriteLine("This is demo line 2"); sw.WriteLine("This is demo line 3"); }Now use the ReadAllLines() method to read all the lines. With that, the Length property is to be used to get the count of lines −int count = File.ReadAllLines("hello.txt").Length;Here is the complete code −Exampleusing System; using System.Collections.Generic; using System.IO; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter("hello.txt")) { ...
Read MoreC# Program to Check Whether the Entered Number is an Armstrong Number or Not
For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.For example, 153 is equal to −1³ + 3³ + 5³To check for it using C#, check the value and find its remainder. Here “val” is the number you want to check for Armstrong −for (int i = val; i > 0; i = i / 10) { rem = i % 10; sum = sum + rem*rem*rem; }Now compare the addition with the actual value. If it matches, that would mean the ...
Read MoreWhat is method hiding in C#?
Method hiding is also known as shadowing. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.Use the new keyword to perform shadowing.Let us see an example.Exampleusing System; using System.Collections.Generic; class Demo { public class Parent { public string GetInfo () { return "This is Parent Class!"; } } public class Child : Parent { public new string GetInfo() { return "This is Child Class!"; } } static void Main(String[] args) { Child child = new Child(); Console.WriteLine(child. GetInfo()); } }OutputThis is Child Class!
Read MoreC# Program to check whether a directory exists or not
Use the Directory. Exists method to check whether a directory exists or not.Let’s say you need to check whether the following directory exists or not −C:\AmitFor that, use the Exists() method −if (Directory.Exists("C:\Amit")) { Console.WriteLine("Directory Amit Exist!"); }The following is the complete code −Exampleusing System.IO; using System; public class Program { public static void Main() { if (Directory.Exists("C:\Amit")) { Console.WriteLine("Directory Amit Exist!"); } else { Console.WriteLine("Directory Amit does not exist!"); } } }OutputDirectory Amit does not exist!
Read MoreC# program to count the number of words in a string
Let us first declare the string −string str = "Hello World!";Now loop through the complete string and find the whitespace or tab or newline character −while (a
Read MoreC# program to convert binary string to Integer
Use the Convert.ToInt32 class to fulfill your purpose of converting a binary string to an integer.Let’s say our binary string is −string str = "1001";Now each char is parsed −try { //Parse each char of the passed string val = Int32.Parse(str1[i].ToString()); if (val == 1) result += (int) Math.Pow(2, str1.Length - 1 - i); else if (val > 1) throw new Exception("Invalid!"); } catch { throw new Exception("Invalid!"); }Check the above for each character in the passed string i.e. “100” using a for a loop. Find the length of ...
Read MoreWhat is the difference between a mutable and immutable string in C#?
Mutable stringStringBuilder is a mutable string in C#. With StringBuilder, you can expand the number of characters in the string. The string cannot be changed once it is created, but StringBuilder can be expanded. It does not create a new object in the memory.Set StringBuilder −StringBuilder str = new StringBuilder();Let us see an example to learn how to work with StringBuilder in C# −Exampleusing System; using System.Text; public class Program { public static void Main() { StringBuilder str = new StringBuilder("Web World!!", 30); str.Replace("World", "Arena"); Console.WriteLine(str); } ...
Read MoreC# Program to get the last write time of a file
To get the last write time of a file in C#, use the LastWriteTime() method.For this, use the FileInfo as well as DateTime classes.Create an object of each −FileInfo file = new FileInfo("amit.txt"); DateTime dt = file.CreationTime; dt = file.LastWriteTime;Let us see the complete code −Exampleusing System.IO; using System; public class Program { public static void Main() { using (StreamWriter sw = new StreamWriter("amit.txt")) { sw.WriteLine("Welcome!"); } FileInfo file = new FileInfo("amit.txt"); // file creation time DateTime dt = file.CreationTime; ...
Read More