Server Side Programming Articles - Page 1421 of 2650

How to implement Single Responsibility Principle using C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:37:34

370 Views

A class should have only one reason to change.Definition − In this context, responsibility is considered to be one reason to change.This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and if in the future we need to make one change we are going to make it in the class which handles it. When we need to make a change in a class having more responsibilities the change might affect the other functions related to the other responsibility of ... Read More

How to get formatted JSON in .NET using C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:35:03

4K+ Views

Use Namespace Newtonsoft.Json.Formatting Newtonsoft.Json.Formatting provides formatting options to Format the JsonNone − No special formatting is applied. This is the default.Indented − Causes child objects to be indented according to the Newtonsoft.Json.JsonTextWriter.Indentation and Newtonsoft.Json.JsonTextWriter.IndentChar settings.Examplestatic void Main(string[] args){    Product product = new Product{       Name = "Apple",       Expiry = new DateTime(2008, 12, 28),       Price = 3.9900M,       Sizes = new[] { "Small", "Medium", "Large" }    };    string json = JsonConvert.SerializeObject(product, Formatting.Indented);    Console.WriteLine(json);    Product deserializedProduct = JsonConvert.DeserializeObject(json);    Console.ReadLine(); } class Product{    public String[] Sizes ... Read More

How to run multiple async tasks and waiting for them all to complete in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:33:01

3K+ Views

The Task.WaitAll blocks the current thread until all other tasks have completed execution.The Task.WhenAll method is used to create a task that will complete if and only if all the other tasks have complete. In the 1st example, we could see that when using Task.WhenAll the task complete is executed before the other tasks are completed. This means that Task.WhenAll doesn’t block the execution. And in the 2nd example, we could see that when using Task.WaitAll the task complete is executed only after all the other tasks are completed. This means that Task.WaitAll block the execution.Examplestatic void Main(string[] args){   ... Read More

What is the difference between All and Any in C# Linq?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:30:58

2K+ Views

Any() method returns true if at least one of the elements in the source sequence matches the provided predicate. Otherwise, it returns false. On the other hand, the All() method returns true if every element in the source sequence matches the provided predicate. Otherwise, it returns falseExamplestatic void Main(string[] args){    IEnumerable doubles = new List { 1.2, 1.7, 2.5, 2.4 };    bool result = doubles.Any(val => val < 1);    System.Console.WriteLine(result);    IEnumerable doubles1 = new List { 0.8, 1.7, 2.5, 2.4 };    bool result1 = doubles1.Any(val => val < 1);    System.Console.WriteLine(result1);    Console.ReadLine(); }OutputFalse TrueExamplestatic ... Read More

What is dependency inversion principle and how to implement in C#?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 06:29:13

808 Views

High-level modules should not depend on low-level modules. Both should depend on abstractions.Abstractions should not depend on details. Details should depend on abstractions.This principle is primarily concerned with reducing dependencies among the code modules.ExampleCode Before Dependency Inversionusing System; namespace SolidPrinciples.Dependency.Invertion.Before{    public class Email{       public string ToAddress { get; set; }       public string Subject { get; set; }       public string Content { get; set; }       public void SendEmail(){          //Send email       }    }    public class SMS{       public ... Read More

Count of distinct rectangles inscribed in an equilateral triangle in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:50:15

186 Views

We are an equilateral triangle with side length. The goal is to count the number of distinct rectangles that can be present inside the triangle such that horizontal sides of the rectangle are parallel to the base. Also all end points of the rectangle touch the dots as shown.Let us understand with examplesInput − sides=3Output − Count of distinct rectangles inscribed in an equilateral triangle are − 1Explanation − The figure above shows the rectangle.Input − sides=10Output − Count of distinct rectangles inscribed in an equilateral triangle are − 200Approach used in the below program is as followsAs from the ... Read More

Count of strings where adjacent characters are of difference one in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:42:00

976 Views

We are given a num number as input. The goal is to count the number of possible strings of length num such that all adjacent characters have difference between ascii values as 1.If num is 2 then strings will be “ab”, “ba”, “bc”, “cb”, ……..”yz”, “zy”.Let us understand with examplesInput − num=3Output − Count of strings where adjacent characters are of difference one are − 98Explanation − Some sample strings are: “abc”, “aba”, “cde” …..”xyx”, “zyz”, “xyz”.Input − num=2Output − Count of strings where adjacent characters are of difference one are − 50Explanation − Some sample strings are: “ab”, “ba”, ... Read More

Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:40:23

147 Views

We are given a number num as input. The goal is to find the number of pairs of form (i, j) such that ((num%i)%j)%num is maximized and i and j both are in range [1, num].Let us understand with examplesInput − num=4Output − Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are − 3Explanation − Pairs will be: (3, 2), (3, 3), (3, 4)Input − num=6Output − Count of pairs of (i, j) such that ((n % i) % j) % n is maximized are − 4Explanation − Pairs will be: ... Read More

Count of occurrences of a “1(0+)1” pattern in a string in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:37:52

206 Views

We are given a string str containing 0s, 1s and other alphabets . It also contains patterns of the form “1(0+)1” where 0+ means any number (>0) of consecutive 0s. The goal is to find such patterns ( “1(0+)1” ) inside string str.Let us understand with examplesInput − str = “abb010bb10111011”Output − Count of occurrences of a “1(0+)1” pattern in a string are − 2Explanation − The patterns inside str are highlighted: “abb010bb10111011”, “abb010bb10111011”Input − str = “01001011001001100”Output − Count of occurrences of a “1(0+)1” pattern in a string are − 4Explanation − The patterns inside str are highlighted: “01001011001001100”, ... Read More

Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points in C++

Sunidhi Bansal
Updated on 03-Dec-2020 07:33:06

183 Views

We are given a circle with K equidistant points on its circumference. Also we are given two points A and B. The goal is to count the number of triangles possible using these points such that they have an obtuse angle ACB( angle greater than 90o) inside them. The points A and B are such that A < B.Here K=8, A=2, B=5, count of points=2 (C, C’) such that angle LACB, LAC’B are obtuse.Let us understand with examplesInput − k=10, A=2, B=4Output − Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points are − ... Read More

Advertisements