Found 2628 Articles for Csharp

How to create a comma separated string from a list of string in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 10:56:42

9K+ Views

A List of string can be converted to a comma separated string using built in string.Join extension method.string.Join(", " , list);This type of conversion is really useful when we collect a list of data (Ex: checkbox selected data) from the user and convert the same to a comma separated string and query the database to process further.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          List fruitsList = new List {             "banana",             ... Read More

How to verify an exception that has been thrown in unit testing C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 09:07:06

4K+ Views

There are two ways that we can verify an exception in unit testing.Using Assert.ThrowsExceptionUsing ExpectedException Attribute.ExampleLet us consider a StringAppend method which throws an exception needs to be tested.using System; namespace DemoApplication {    public class Program {       static void Main(string[] args) {       }       public string StringAppend(string firstName, string lastName) {          throw new Exception("Test Exception");       }    } }Using Assert.ThrowsExceptionusing System; using DemoApplication; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace DemoUnitTest {    [TestClass]    public class DemoUnitTest {       [TestMethod]       public void ... Read More

How to convert byte array to an object stream in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 09:01:09

2K+ Views

Stream is the abstract base class of all streams and it Provides a generic view of a sequence of bytes. The Streams Object involve three fundamental operations such as Reading, Writing and Seeking. A stream be can be reset which leads to performance improvements.A byte array can be converted to a memory stream using MemoryStream Class.MemoryStream stream = new MemoryStream(byteArray);ExampleLet us consider a byte array with 5 values 1, 2, 3, 4, 5. Live Demousing System; using System.IO; namespace DemoApplication {    class Program {       static void Main(string[] args) {          byte[] byteArray = new ... Read More

What is the difference between Func delegate and Action delegate in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:59:02

2K+ Views

A delegate is a type that represents references to methods with a particular parameter list and return type. When we instantiate a delegate, we can associate its instance with any method with a compatible signature and return type. We can invoke (or call) the method through the delegate instance.Func DelegateFunc is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter. This delegate can point to a method that takes up to 16 Parameters and returns a value.Below is the Func delegate ... Read More

How to validate whether a string is a number in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:56:21

1K+ Views

A string having number can be validated using int.TryParse or int.Parse.Int.Parse throws an exception if it cannot parse the string to an integer, whereas Int.TryParse returns a bool indicating whether it succeeded. Also, Int.TryParse has an out parameter which has the value of the parsed string.Example Live Demousing System; namespace DemoApplication {    class Program {       static void Main(string[] args) {          string numberString = "123";          int number = 0;          if(int.TryParse(numberString, out number)) {             Console.WriteLine($"Try Parse Interger Number: {number}");     ... Read More

How to create a folder if it does not exist in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:52:58

10K+ Views

For creating a directory, we must first import the System.IO namespace in C#. The namespace is a library that allows you to access static methods for creating, copying, moving, and deleting directories.It is always recommended to check if the Directory exist before doing any file operation in C# because the complier will throw exception if the folder does not exist.Exampleusing System; using System.IO; namespace DemoApplication {    class Program {       static void Main(string[] args) {          string folderName = @"D:\Demo Folder";          // If directory does not exist, create it   ... Read More

How to convert C# DateTime to “YYYYMMDDHHMMSS” format?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:49:41

4K+ Views

Convert the dateTime to toString that results in converting the DateTime to “YYYYMMDDHHMMSS” formatThere are also other formats that the dateTime can be convertedMM/dd/yyyy 08/22/2020dddd, dd MMMM yyyy Tuesday, 22 August 2020dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2020 06:30dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2020 06:30 AMdddd, dd MMMM yyyy H:mm Tuesday, 22 August 2020 6:30dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2020 6:30 AMdddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2020 06:30:07MM/dd/yyyy HH:mm 08/22/2020 06:30MM/dd/yyyy hh:mm tt 08/22/2020 06:30 AMMM/dd/yyyy H:mm 08/22/2020 6:30MM/dd/yyyy h:mm tt 08/22/2020 6:30 AMMM/dd/yyyy HH:mm:ss 08/22/2020 06:30:07Example Live ... Read More

How to check if a number is a power of 2 in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:49:09

498 Views

A power of 2 is a number of the form 2n where n is an integerThe result of exponentiation with number two as the base and integer n as the exponent.n2n01122438416532Example 1 Live Democlass Program {    static void Main() {       Console.WriteLine(IsPowerOfTwo(9223372036854775809));       Console.WriteLine(IsPowerOfTwo(4));       Console.ReadLine();    }    static bool IsPowerOfTwo(ulong x) {       return x > 0 && (x & (x - 1)) == 0;    } }OutputFalse TrueExample 2 Live Democlass Program {    static void Main() {       Console.WriteLine(IsPowerOfTwo(9223372036854775809));       Console.WriteLine(IsPowerOfTwo(4));       Console.ReadLine();    }    static bool IsPowerOfTwo(ulong n) {       if (n == 0)          return false;       while (n != 1) {          if (n % 2 != 0)             return false;          n = n / 2;       }       return true;    } }OutputFalse True

How do you do a deep copy of an object in .NET?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:44:12

92 Views

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicatedDeep Copy is used to make a complete deep copy of the internal reference types.In another words a deep copy occurs when an object is copied along with the objects to which it refersExample Live Democlass DeepCopy {    public int a = 10; } class Program {    static void Main() {       //Deep Copy       DeepCopy d = new DeepCopy();       d.a = 10;       DeepCopy d1 = new ... Read More

What does the [Flags] Enum Attribute mean in C#?

Nizamuddin Siddiqui
Updated on 08-Aug-2020 08:41:57

502 Views

The Enum Flags is used to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single valueUse the FlagsAttribute for an enumeration only if a bitwise operation (AND, OR, EXCLUSIVE OR) is to be performed on a numeric value.Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap.Example Live Democlass Program {    [Flags]    enum SocialMediaFlags { None = 0, Facebook = 1, Twitter = ... Read More

Advertisements