Found 2628 Articles for Csharp

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

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

490 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

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

449 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

360 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

351 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

608 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

320 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

How do interfaces work in C#?

Akshay Khot
Updated on 19-May-2021 07:47:44

439 Views

An interface defines a contract that will be implemented by a class or a struct. It can contain methods, properties, events, and indexers. An interface is similar to a class except that it doesn't hold any data and only specifies the behavior it can do (or more accurately, the class that implements it can do).A class can implement one or more interfaces. To implement an interface member, the class should have a public member with the same method definition as the interface member, i.e. same name and signature.For example, IComparer is an interface defined in the System.Collections namespace that defines ... Read More

What is the usage of ref, out, and in keywords in C#?

Akshay Khot
Updated on 19-May-2021 07:41:00

406 Views

In C#, most of the methods can have zero or more parameters which define the data that must be provided to the method. Any code that calls the method has to pass the data (called arguments) to the method. A method declares its inputs as parameters, and they're provided by calling code in the form of arguments.For example, consider the following method and subsequent method call.static void Greet(string greeting){    Console.WriteLine(greeting); } ... Greet("Hello");In the above example, greeting is a parameter of the Greet() method, and "Hello" is an argument passed to the method.When you call a method and pass ... Read More

Provide a brief overview of the C# and .NET ecosystem

Akshay Khot
Updated on 19-May-2021 07:40:00

483 Views

C# is an object-oriented, type-safe and general-purpose programming language, which focuses on making the programmers productive. It tries to achieve this productivity through expressiveness, simplicity and a focus on performance. It works on different platforms such as Windows, Mac, and Linux.Type-SafetyC# is a statically typed language. That means the types are verified when you compile a program. This eliminates a large set of errors before the program even runs.Garbage CollectionAutomatic memory management is an essential feature of C#. It has a garbage collector that runs along with the programs, reclaiming the unused memory. This frees the burden from programmers to ... Read More

Advertisements