Found 2587 Articles for Csharp

How to convert XML to Json and Json back to XML using Newtonsoft.json?

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:32:31

3K+ Views

Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter.Elements, attributes, text, comments, character data, processing instructions, namespaces, and the XML declaration are all preserved when converting between the twoSerializeXmlNodeThe JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode(). This method takes an XmlNode and serializes it to JSON text.DeserializeXmlNodeThe second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into an XmlNode.Example 1static void Main(string[] args) {    string xml = @"Alanhttp://www.google1.com Admin1";    XmlDocument doc = new XmlDocument();    doc.LoadXml(xml);    string json = JsonConvert.SerializeXmlNode(doc); ... Read More

How to resize an Image C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:14:52

582 Views

A bitmap consists of the pixel data for a graphics image and its attributes. There are many standard formats for saving a bitmap to a file. GDI+ supports the following file formats: BMP, GIF, EXIF, JPG, PNG and TIFF. You can create images from files, streams, and other sources by using one of the Bitmap constructors and save them to a stream or to the file system with the Save method.In the below code CompressAndSaveImageAsync Method Compresses the images and saves in the path Mentioned.The new image name will be a combination of desktop userId and dateTimeExampleprivate async Task CompressAndSaveImageAsync(Bitmap ... Read More

How can I limit Parallel.ForEach in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:12:44

2K+ Views

Parallel ForeachParallel.ForEach loop in C# runs upon multiple threads and processing takes place in a parallel way. Parallel.ForEach loop is not a basic feature of C# and it is available from C# 4.0 and above To use Parallel.ForEach loop we need to import System.Threading.Tasks namespace in using directive.ForeachForeach loop in C# runs upon a single thread and processing takes place sequentially one by one. Foreach loop is a basic feature of C# and it is available from C# 1.0. Its execution is slower than the Parallel.Foreach in most of the cases.Example 1static void Main(string[] args){    List alphabets = new ... Read More

How to determine if C# .NET Core is installed?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:22:15

3K+ Views

The following options are for dotnet by itself. For example, dotnet −−info. They print out information about the environment if not installed it will throw error.−−infoPrints out detailed information about a .NET Core installation and the machine environment, such as the current operating system, and commit SHA of the .NET Core version.−−versionPrints out the version of the .NET Core SDK in use.−−list−runtimesPrints out a list of the installed .NET Core runtimes. An x86 version of the SDK lists only x86 runtimes, and an x64 version of the SDK lists only x64 runtimes.−−list−−sdksPrints out a list of the installed .NET Core ... Read More

What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:04:17

5K+ Views

DateTimeDateTime is a structure of value Type like int, double etc. It is available in System namespace and present in mscorlib.dll assembly.It implements interfaces like IComparable, IFormattable, IConvertible, ISerializable, IComparable, IEquatable.DateTime contains properties like Day, Month, Year, Hour, Minute, Second, DayOfWeek and others in a DateTime object.TimeSpanTimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds.TimeSpan is used to compare two DateTime objects to find the difference between two dates. TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds methods to create TimeSpan objects from days, hours, minutes, seconds, ... Read More

How to easily initialize a list of Tuples in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:03:01

4K+ Views

Tuple can be used where you want to have a data structure to hold an object with properties, but you don't want to create a separate type for it. The Tuple class was introduced in .NET Framework 4.0. A tuple is a data structure that contains a sequence of elements of different data types.Tuple person = new Tuple (1, "Test", "Test1");A tuple can only include a maximum of eight elements. It gives a compiler error when you try to include more than eight elements.Tuples of Listvar tupleList = new List {    (1, "cow1"),    (5, "chickens1"),    (1, "airplane1") ... Read More

How to parse a string into a nullable int in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:01:07

6K+ Views

C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values.C# 2.0 introduced nullable types that allow you to assign null to value type variables. You can declare nullable types using Nullable where T is a type.Nullable types can only be used with value types.The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value.The HasValue property returns true if the variable contains a value, or false if it is null.You can only use == and != operators with a nullable type. ... Read More

What is an alternative to string.Replace that is case-insensitive in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 12:01:40

3K+ Views

Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.Example 1To replace even the case sensitive charaters Regular expressions provide a powerful, flexible, and efficient method for processing text. The extensive pattern-matching notation of regular expressions enables you to quickly parse large amounts of text to:Find specific character patterns.Validate text to ensure that it matches a predefined pattern (such as an email address).Extract, edit, replace, or delete text substrings.Add ... Read More

What are the benefits to marking a field as readonly in C#?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:58:05

555 Views

The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the field either in the declaration, or in the constructor of the object that the field is a member of.The 'readonly' modifier can be used in a total of four contexts:Field declarationReadonly struct declarationReadonly member definitionRef read only method returnWhen we use the field declaration context, we need to know that the ... Read More

How to use String Format to show decimal up to 2 places or simple integer in C#?

Nizamuddin Siddiqui
Updated on 05-Nov-2020 13:56:19

4K+ Views

Converts the value of objects to strings based on the formats specified and inserts them into another string.Namespace:System Assembly:System.Runtime.dllEach overload of the Format method uses the composite formatting feature to include zero-based indexed placeholders, called format items, in a composite format string. At run time, each format item is replaced with the string representation of the corresponding argument in a parameter list. If the value of the argument is null, the format item is replaced with String.Empty.Exampleclass Program{    static void Main(string[] args){       int number = 123;       var s = string.Format("{0:0.00}", number);     ... Read More

Advertisements