Replace Line Breaks in a String in C#

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

2K+ Views

Let us take we have to eliminate the line breaks, space and tab space from the below string.eliminate.jpgExampleWe can make use of Replace() extension method of string to do it. Live Demousing System; namespace DemoApplication {    class Program {       static void Main(string[] args) {          string testString = "Hello \r beautiful \t world";          string replacedValue = testString.Replace("\r", "_").Replace("\t", "_");          Console.WriteLine(replacedValue);          Console.ReadLine();       }    } }OutputThe output of the above code isHello _ beautiful _ worldExampleWe can also make use ... Read More

Update Values of a Collection Using LINQ in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:19:03

13K+ Views

If the collection is a List, then we can make use of ForEach extension method which is available as part of LINQ.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static void Main(string[] args) {          List fruits = new List {             new Fruit {                Name = "Apple",                Size = "Small"             },             new Fruit {         ... Read More

Difference Between int.Parse and Convert.ToInt32 in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:14:17

6K+ Views

Convert a string representation of number to an integer, using the int.Parse or Convert.ToInt32 method in C#. If the string cannot be converted, then the int.Parse or Convert.ToInt32 method returns an exceptionConvert.ToInt32 allows null value, it doesn't throw any errors Int.parse does not allow null value, and it throws an ArgumentNullException error.Example Live Democlass Program {    static void Main() {       int res;       string myStr = "5000";       res = int.Parse(myStr);       Console.WriteLine("Converting String is a numeric representation: " + res);       Console.ReadLine();    } }OutputConverting String is a ... Read More

Delete All Files and Folders from a Path in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:12:07

3K+ Views

For deleting all the folders and its respective directories we can make us System.IO namespace available in C#. The DirectoryInfo() class provides the details of all sub directories and file in a directory.ExampleLet us consider a directory Demo having two sub directories and has some files like below.using System.IO; namespace DemoApplication {    class Program {       static void Main(string[] args) {          DirectoryInfo di = new DirectoryInfo(@"D:\Demo");          foreach (DirectoryInfo dir in di.GetDirectories()) {             foreach (FileInfo file in dir.GetFiles()) {           ... Read More

Fetch Property Value Dynamically in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:08:45

7K+ Views

We can make use of Reflection to fetch a property value dynamically.Reflection provides objects (of type Type) that describe assemblies, modules and types. We can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If we use attributes in our code, reflection enables us to access them.The System.Reflection namespace and System.Type class play an important role in .NET Reflection. These two works together and allows us to reflect over many other aspects of a ... Read More

Convert Byte Array to String in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:05:15

19K+ Views

In .Net, every string has a character set and encoding. A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters. Actually, it is the process of transforming a set of Unicode characters into a sequence of bytes.We can use Encoding.GetString Method (Byte[]) to decodes all the bytes in the specified byte array into a string. Several other decoding schemes are also available in Encoding class such as UTF8, Unicode, UTF32, ASCII etc. The Encoding class is available as part of System.Text namespace.string result = Encoding.Default.GetString(byteArray);Example Live ... Read More

Handle Collection Modified Error in C#

Nizamuddin Siddiqui
Updated on 08-Aug-2020 11:02:45

20K+ Views

This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or removed) during the runtime.Example Live Demousing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          try {             var studentsList = new List {                new Student {                   Id = 1,                   Name = "John"                },                new Student {                   Id = 0,                   Name = "Jack"                },                new Student {                   Id = 2,                   Name = "Jack"                }             };             foreach (var student in studentsList) {                if (student.Id

Create Comma Separated String from a List of String in C#

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

13K+ 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

Verify Exception Thrown in Unit Testing with 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

Convert Byte Array to 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

Advertisements