George John has Published 1167 Articles

C# program to find common values from two or more Lists

George John

George John

Updated on 22-Jun-2020 13:00:17

249 Views

Create more than one list −// two lists var list1 = new List{3, 4}; var list2 = new List{1, 2, 3};Now, use the Intersect() method to get the common values −var res = list1.Intersect(list2);The following is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo ... Read More

C# Truncate Method

George John

George John

Updated on 22-Jun-2020 12:45:15

276 Views

Use the Truncate method in C# to remove all the numbers after decimal places.Let’s say the following is our number −20.35MTo remove the numbers after decimal places, use Truncate() −decimal.Truncate(20.35M)Let us see the comple code −Exampleusing System; using System.Linq; class Demo {    static void Main() {     ... Read More

How to open hidden file using C#?

George John

George John

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

505 Views

To open a hidden file, first make it visible. You can do this by removing the hidden attribute set on it −FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt"); file.Attributes &= ~FileAttributes.Hidden;Now treat it as a normal text file and open it. Read the content −using (StreamReader sr = new StreamReader("myFile.txt")) { ... Read More

File Objects in C#

George John

George John

Updated on 22-Jun-2020 12:35:19

820 Views

To create a new file in C#, use the FileStream object.The following is the syntax −FileStream = new FileStream( , , , );Let us see an example for a file “test.dat”, which is created/ opened using File object −FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);The following is an example ... Read More

How to List all Substrings in a given String using C#?

George John

George John

Updated on 22-Jun-2020 12:27:41

538 Views

To list all the substrings, use the Substring method and loop through the length of the string.Let’s say our string is −string myStr = "pqrz";Use nested loop and get the substring in a new string −for (int i = 1; i < myStr.Length; i++) {    for (int start = 0; start

How to insert an item into a C# list by using an index?

George John

George John

Updated on 22-Jun-2020 12:22:30

2K+ Views

Firstly, set a list −List list = new List(); list.Add(456); list.Add(321); list.Add(123); list.Add(877); list.Add(588); list.Add(459);Now, to add an item at index 5, let say; for that, use the Insert() method −list.Insert(5, 999);Let us see the complete example −Exampleusing System; using System.Collections.Generic; namespace Demo {    public class Program { ... Read More

How to instantiate delegates in C#?

George John

George John

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

957 Views

Use the new keyword to instantiate a delegate. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.For example −public delegate void printString(string s); printString ps1 = new printString(WriteToScreen);You can also instantiate a delegate using ... Read More

How can a user implicitly end current MySQL transaction?

George John

George John

Updated on 22-Jun-2020 11:23:29

76 Views

Following are the ways with the help of which current MySQL transaction can be ended implicitly −By Running DDL statementThe current MySQL transaction will end implicitly and changes will be committed by running any of the DDL statement such as CREATE or DROP databases, Create, ALTER or DROP tables or ... Read More

How to convert a string into int in C#?

George John

George John

Updated on 22-Jun-2020 11:00:34

245 Views

Let’s say our string is −string str ="9999";Now, use the Int32.Parse() to convert the string into integer −int n = Int32.Parse(str);Now display the integer value as shown in the following code −Exampleusing System; class Demo {    static void Main() {       string str ="9999";       ... Read More

How to find the file using C#?

George John

George John

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

89 Views

Use the GetDirectories in C# to get a list of sub-folder that appears first −Directory.GetDirectoriesNow loop through those directories and repeat the process for the sub folder.string path = @"d:/New/Myfile"; string[] myDir = Directory.GetDirectories(path, "xml", SearchOption.AllDirectories); Console.WriteLine(myDir.Length.ToString()); foreach (string res in myDir) Console.WriteLine(res);Read More

Advertisements