Difference Between const and readonly Keywords in C#

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

801 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

Operators Chash Provides to Deal with Null Values

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

409 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

575 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

Usage of ref, out, and in Keywords in C#

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

564 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

Overview of the C# and .NET Ecosystem

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

687 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

Explain and Contrast Value Types and Reference Types in C#

Akshay Khot
Updated on 19-May-2021 07:33:11

342 Views

In general, all types in C# can be divided into two main categories − value types and reference types. Let's look at each type in detail.Value TypesVariables of value types directly contain their data. Each variable has its own copy of the data. Hence it is impossible for a variable of value type to modify another object.A value type can be one of the following types −all numeric types, e.g., int, float, and doublechar and the bool typesstruct type orenumeration type.A value type simple contains the value. For example, the integer type contains the actual number, and not a pointer ... Read More

Dot Product of Two Sparse Vectors in Python

Arnab Chakraborty
Updated on 18-May-2021 12:14:56

704 Views

Suppose, we have two sparse vectors represented in two lists. We have to return the dot product of the two sparse vectors. The vectors are represented as objects, and the lists are stored in a member variable 'nums' in the objects.So, if the input is like vector1 = [1, 0, 0, 0, 1], vector2 = [0, 0, 0, 1, 1], then the output will be 1 The dot product is 1 * 0 + 0 * 0 + 0 * 0 + 0 * 1 + 1 * 1 = 1.To solve this, we will follow these steps −res := ... Read More

Find Number of Boxes to Put into Godown in Python

Arnab Chakraborty
Updated on 18-May-2021 12:14:14

376 Views

Suppose, we have two arrays containing integers. One list contains the height of some unit width boxes and another array contains the height of rooms in the godown. The rooms are numbered 0...n, and the height of the rooms is provided in their respective indexes in the array godown. We have to find out the number of boxes that can be pushed into the godown. a few things have to be kept in mind, The boxes can’t be put one on another.The order of the boxes can be changed.The boxes are put into the godown from left to right only.If ... Read More

Check If Strings Differ by One Character in Python

Arnab Chakraborty
Updated on 18-May-2021 12:12:02

186 Views

Suppose, we are provided an array that contains several strings that are of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return true or else we return false.So, if the input is like dict = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output produced is True because the strings listed in the input all differ in index 1. So, if any two pairs are taken, there is a difference in the same position.To solve this, ... Read More

Find Index of Most Frequent Element in Concealed Array in Python

Arnab Chakraborty
Updated on 18-May-2021 12:08:02

268 Views

Suppose, we are given a class called 'TestArray' that contains an private array which can only contain values 0 or 1; and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four values p, q, r, s as input and works like this −if all the four values in the given indexes of the array are either 0 or 1, it returns 4.else if any three values in the given indexes of the array are the ... Read More

Advertisements