Found 2587 Articles for Csharp

How to find all unique triplets that adds up to sum Zero using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 07:03:40

374 Views

The easy approach is that we could create three nested loops and check one by one that the sum of all the three elements is zero or not. If the sum of the three elements is zero then print elements.Time Complexity − O(n3)Space Complexity − O(1)We could use an unordered set data structure to store each value of the array. Set provides the benefit of searching an element in O(1) time. So, for each pair in the array, we will look for the negative of their sum that might exist in the set. If such an element is found then ... Read More

How to search in a row wise increased matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:49:57

200 Views

The primitive solution for this problem is to scan all elements stored in the input matrix to search for the given key. This linear search approach costs O(MN) time if the size of the matrix is MxN.The matrix needs to be scanned from the top right, if the search element is greater than the top right element then increments the row or else decrement the column. The below piece of code develops a function SearchRowwiseIncrementedMatrix that takes a two-dimensional array and search key as input and returns either true or false depending upon the success or failure of search key ... Read More

How to search in a row wise and column wise increased matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:46:07

250 Views

The primitive solution for this problem is to scan all elements stored in the input matrix to search for the given key. This linear search approach costs O(MN) time if the size of the matrix is MxN.The matrix can be viewed as a sorted one-dimensional array. If all rows in the input matrix are concatenated in top-down order, it forms a sorted one-dimensional array. And, in that case binary search algorithm is suitable for this 2D array. The below piece of code develops a function SearchRowwiseColumnWiseMatrix that takes a two-dimensional array and search key as input and returns either true ... Read More

How to check whether the given matrix is a Toeplitz matrix using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 06:39:33

679 Views

A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.Example 1[[1, 2, 3, 4], [5, 1, 2, 3], [9, 5, 1, 2]]Output −trueIn the above grid, the diagonals are −"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".In each diagonal all elements are the same, so the answer is True.Example 2Input: matrix [[1, 2], [2, 2]]Output −falseThe diagonal "[1, 2]" has different elementsCodepublic class Matrix    {    public bool ToeplitzMatrix(int[, ] mat)    {       int row = getMatrixRowSize(mat);       int col = getMatrixColSize(mat);       ... Read More

Explain dependency injection in C#

Akshay Khot
Updated on 19-May-2021 08:18:30

16K+ Views

A dependency is an object that another object depends on. Dependency Injection (or inversion) is basically providing the objects that an object needs, instead of having it construct the objects themselves. It is a useful technique that makes testing easier, as it allows you to mock the dependencies.For example, if class A calls a method on class B, which in turn calls a method on class C, that means A depends on B and B depends on C. Using dependency injection, we can pass an instance of class C to class B, and pass an instance of B to class ... Read More

System.Reflection namespace in C#

Akshay Khot
Updated on 19-May-2021 08:17:54

616 Views

System.Reflection namespace in C# The System.Reflection namespace in C# contains the types that provide information about assemblies, modules, members, parameters, and other items in the code by examining the metadata. The Assembly class in this namespace represents an assembly. Typically, you can access it using the Assembly property on a Type.An assembly's identity consists of four items −Simple nameVersion from the AssemblyVersion attribute in the major.minor.build.revision format (0.0.0.0 if absent)Culture (neutral if not a satellite)Public key token (null if not strongly named)A fuller qualified assembly name is a string, and it includes these identifying items in the format −simple-name, Version=version, ... Read More

What is the purpose of the StringBuilder class in C#?

Akshay Khot
Updated on 19-May-2021 08:11:13

472 Views

In C#, strings are immutable. That means you cannot modify a string once it's created. Any modification to the string returns a new string that contains the modification, leaving the original string intact.string word = "aaabbbccc"; string newWord = word.Replace('b', 'd'); Console.WriteLine(word); // prints aaabbbccc Console.WriteLine(newWord); // prints aaadddcccStringBuilder class represents a string-like object that can be modified, i.e. a mutable string of characters. It's implemented differently than the string type, which represents an immutable string of characters.As modifying a string object creates a copy, repeatedly modifying string objects can incur a performance penalty. For small repetitions it's negligible, but ... Read More

What are some of the important namespaces in C#? Provide a brief description of each

Akshay Khot
Updated on 19-May-2021 08:07:39

628 Views

.NET contains a lot of namespaces and many more, if you include the third-party libraries. However, there are a few that you will use over and over. Here are the twenty that will get you through 80% of the common, recurring programming problems.SystemContains the most fundamental types. These include commonly used classes, structures, enums, events, interfaces, etc.System.TextContains classes that represent ASCII and Unicode character encodings. Classes for converting blocks of characters to and from blocks of bytes.System.Text.RegularExpressionsProvides regular expression functionality.System.LinqProvides classes and interfaces that support queries that use Language-Integrated Query (LINQ).System.XML.LinqContains the classes for LINQ to XML. LINQ to XML ... Read More

Explain the difference between const and readonly keywords in C#

Akshay Khot
Updated on 19-May-2021 07:50:10

780 Views

In C#, both the const and readonly keywords are used to define immutable values which cannot be modified once they are declared. However, there are some important differences between the two.constThe const modifier declares the constant values that are known at compile-time and do not change, i.e. they are immutable. In C#, you can mark only the built-in types as const. User-defined types such as classes, structs, etc. cannot be const. Also, class member types such as methods, properties, or events cannot be marked as constants.You must initialize the constants during declaration.class Period{    public const int hours = 12; ... Read More

What operators C# provides to deal with null values?

Akshay Khot
Updated on 19-May-2021 07:49:08

401 Views

C# has the following three operators to deal with the null values −null-coalescing operator (??)Allows you to get the value of a variable if it's not null, alternatively specifying a default value that can be used.It replaces the following expression in C# −string resultOne = value != null ? value : "default_value";with the following expression −string resultTwo = value ?? "default_value";Here is an example that illustrates this.Exampleusing System; class Program{    static void Main(){       string input = null;       string choice = input ?? "default_choice";       Console.WriteLine(choice); // default_choice       string ... Read More

Advertisements