C# ofType Method

Arjun Thakur
Updated on 23-Jun-2020 07:12:57

609 Views

Filter a collection on the basis of each of its elements type.Let’s say you have the following list with integer and string elements −list.Add("Katie"); list.Add(100); list.Add(200);To filter collection and get only elements with string type.var myStr = from a in list.OfType() select a;Work the same for integer type.var myInt = from a in list.OfType() select a;The following is the complete code −Example Live Demousing System; using System.Linq; using System.Collections; public class Demo {    public static void Main() {       IList list = new ArrayList();       list.Add("Katie");       list.Add(100);       list.Add(200);     ... Read More

Delete a File in C#

Ankith Reddy
Updated on 23-Jun-2020 07:12:15

941 Views

Use File.Delete method to delete a file.Firstly, set the path of the file you want to delete.String myPath = @"C:\New\amit.txt";Now, use the File.Delete method to delete the file.File.Delete(myPath);The following is the complete code −Example Live Demousing System; using System.IO; public class Program {    public static void Main() {       String myPath = @"C:\New\amit.txt";       Console.WriteLine("Deleting File");       File.Delete(myPath);    } }OutputDeleting File

C# Program to Make a Copy of an Existing File

Samual Sam
Updated on 23-Jun-2020 07:11:53

978 Views

Use File.Copy method to make copy of an existing file.Add the path of the file you want to copy.String myPath = @"D:\one.txt";Now copy the above file to the following file −String myPath = @"D:\one.txt";Use the File.Copy method with both the source and destination file.File.Copy(myPath,newpath);Exampleusing System; using System.IO; public class Program {    public static void Main() {       String myPath = @"D:\one.txt";       // the file will get copied here       String newpath = @"D:\two.txt";       // copying file       File.Copy(myPath,newpath);    } }

E and E Custom Specifiers in C#

George John
Updated on 23-Jun-2020 07:11:31

432 Views

If any of the following string appears in the format string and followed by at least one zero, then the number is formatted with an “E” or “e” in between the number and the exponent."E" "E+" "E-" "e" "e+" "e-"Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double num = 9400;       Console.WriteLine(num.ToString("0.###E+0", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.###E+0}", num));       Console.WriteLine(num.ToString("0.###E+000", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.###E+000}", num));    } }Output9.4E+3 9.4E+3 9.4E+003 9.4E+003

Add Node at First Position in a Linked List Using C#

Chandu yadav
Updated on 23-Jun-2020 07:10:46

437 Views

Firstly, set a LinkedList with nodes.string [] students = {"Tim", "Jack", "Henry", "David", "Tom"}; LinkedList list = new LinkedList(students);To add a node at the first position, use the AddFirst() method.list.AddFirst("Amit");Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] students = {"Tim", "Jack", "Henry", "David", "Tom"};       LinkedList list = new LinkedList(students);       foreach (var stu in list) {          Console.WriteLine(stu);       }       // adding a node       Console.WriteLine("LinkedList after adding a node at the first position...");   ... Read More

Get Smallest and Largest Element from a List in C#

George John
Updated on 23-Jun-2020 07:09:34

10K+ Views

Set a list.List list = new List { 150, 300, 400, 350, 450, 550, 600 };To get the smallest element, use the Min() method.list.AsQueryable().Min();To get the largest element, use the Max() method.list.AsQueryable().Max();Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       List list = new List { 150, 300, 400, 350, 450, 550, 600 };       foreach(long ele in list){          Console.WriteLine(ele);       }       // getting largest element       long max_num = list.AsQueryable().Max(); ... Read More

Return Total Elements in a Sequence as 64-bit Signed Integer in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:08:30

103 Views

Firstly, set a string array.string[] num = { "One", "Two", "Three", "Four", "Five"};Use the Linq LongCount method to get the count of elements.num.AsQueryable().LongCount();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       string[] num = { "One", "Two", "Three", "Four", "Five"};       long res = num.AsQueryable().LongCount();       Console.WriteLine("{0} elements", res);    } }Output5 elements

General Date and Long Time G Format Specifier in C#

Samual Sam
Updated on 23-Jun-2020 07:07:32

269 Views

The General Date Long Time standard format specifier is a combination of the short date ("d") and long time ("T") patterns, separated by a space.Set the date −DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);Now, use the ToString() method and DateTimeFormatInfo.dt.ToString("G", DateTimeFormatInfo.InvariantInfo)Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);       Console.WriteLine(dt.ToString("G",       DateTimeFormatInfo.InvariantInfo));    } }Output01/03/2018 03:45:20

Access Document Properties Using W3C DOM Method

Sravani S
Updated on 23-Jun-2020 07:06:52

175 Views

This IE4 document object model (DOM) introduced in Version 4 of Microsoft's Internet Explorer browser. IE 5 and later versions include support for most basic W3C DOM features.ExampleTo access document properties using W3C DOM method, you can try to run the following code −           Document Title                                     This is main title       Click the following to see the result:                                                          

Object Initializer in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:06:25

186 Views

Initialize an object of a class with object initialize.Using it, you can assign values to the fields at the time of creating an object.We created Employee object and assigned values using curly bracket at the same time.Employee empDetails = new Employee() {    EID = 10,    EmpName = "Tim",    EmpDept = "Finance" }Now access the values of the Employee class. For example, for name of the employee.empDetails.EmpNameLet us see the complete code −Example Live Demousing System; public class Demo {    public static void Main() {       Employee empDetails = new Employee() {         ... Read More

Advertisements