Server Side Programming Articles - Page 2443 of 2646

C# Program to make a copy of an existing file

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

990 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);    } }

C# Linq Where Method

karthikeya Boyini
Updated on 23-Jun-2020 07:13:44

3K+ Views

The Where method filters an array of values based on a predicate.Here, the predicate is checking for elements above 70.Where((n, index) => n >= 70);Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 };       Console.WriteLine("Array:");       foreach (int a in arr)       Console.WriteLine(a);       // getting elements above 70       IEnumerable myQuery = arr.AsQueryable().Where((n, index) => n >= 70);       Console.WriteLine("Elements above 70...:");       foreach (int res in myQuery)       Console.WriteLine(res);    } }OutputArray: 10 30 20 15 90 85 40 75 Elements above 70...: 90 85 75

C# OfType() Method

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

641 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

C# int.Parse Method

Sai Subramanyam
Updated on 23-Jun-2020 07:14:09

24K+ Views

Convert a string representation of number to an integer, using the int.Parse method in C#. If the string cannot be converted, then the int.Parse method returns an exceptionLet’s say you have a string representation of a number.string myStr = "200";Now to convert it to an integer, use the int.Parse(). It will get converted.int.Parse(myStr);Example Live Demousing System.IO; using System; class Program {    static void Main() {       int res;       string myStr = "200";       res = int.Parse(myStr);       Console.WriteLine("String is a numeric representation: "+res);    } }OutputString is a numeric representation: 200

C# Enum IsDefined Method

Chandu yadav
Updated on 23-Jun-2020 07:14:32

2K+ Views

The IsDefined method returns true if a given integral value, or its name as a string, is present in a specified enum.The following is our enum −enum Subjects { Maths, Science, English, Economics };The above is initialized by default i.e.Maths = 0, Science = 1, English = 2, Economics = 3Therefore, when we will find 3 using IsDefined(), then it will return True as shown below −Example Live Demousing System; public class Demo {    enum Subjects { Maths, Science, English, Economics };    public static void Main() {       object ob;       ob = 3;       Console.WriteLine("{0} = {1}", ob, Enum.IsDefined(typeof(Subjects), ob));    } }Output3 = True

Multiple Where clause in C# Linq

karthikeya Boyini
Updated on 23-Jun-2020 07:02:56

5K+ Views

Filter collections using Where clause in C#. A single query expression may have multiple where clauses.Firstly, set a collection −IList employee = new List() {    new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8} ,    new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 } ,    new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 } ,    new Employee() { EmpID = 4, EmpName = "Amy" , EmpMarks = 67, Rank = 20} , };Now, let’s use multiple ... Read More

Action Delegate in C#

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

277 Views

Action delegate does not return a value and can be used with a method that has a void return type.Declare Action Delegate.Action del = Display;Here is our method −public static void Display(int val) {    Console.WriteLine(val); }Now call the method with a value.Example Live Demousing System; public class Demo {    public static void Main() {       Action del = Display;       del(2);    }    public static void Display(int val) {       Console.WriteLine(val);    } }Output2

Collection Initialization in C#

Ankith Reddy
Updated on 23-Jun-2020 07:04:32

910 Views

Initialize Collection like class objects using collection initializer syntax.Firstly, set values for the Employee object −var emp1 = new Employee() { EID = 001, EmpName = "Tim", EmpDept = "Finance"}; var emp2 = new Employee() { EID = 002, EmpName = "Tom", EmpDept = "HR"};Now add this under a collection.IList empDetails = new List {emp1, emp2 };Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       var emp1 = new Employee() { EID = 001, EmpName = "Tim", EmpDept = "Finance"};       var emp2 ... Read More

Short Time ("t") Format Specifier in C#

Samual Sam
Updated on 23-Jun-2020 07:03:57

493 Views

The Short Time format specifier represents a custom date and time format string.It is defined by the current DateTimeFormatInfo.ShortTimePattern property.For example, the custom format string is −HH:mmExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime date = new DateTime(2018, 5, 4, 10, 12, 10);       Console.WriteLine(date.ToString("t", CultureInfo.CreateSpecificCulture("en-us")));    } }Output10:12 AM

Month ("M", "m") Format Specifier in C#

Arjun Thakur
Updated on 23-Jun-2020 07:05:26

364 Views

The Month standard format specifier represents a custom date and time format string.The format string is defined by the current DateTimeFormatInfo.MonthDayPattern property.The custom format string −MMMM ddExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime date = new DateTime(2018, 6, 11, 9, 15, 0);       Console.WriteLine(date.ToString("m", CultureInfo.CreateSpecificCulture("en-us")));    } }OutputJune 11

Advertisements