Found 33676 Articles for Programming

How does the event pattern work in .NET?

Akshay Khot
Updated on 19-May-2021 08:05:42

564 Views

Events are a simplified pattern that uses delegates. In C#, all delegates have the multicast capability, i.e., an instance of a delegate can represent not just a single method but a list of methods. For example −Exampledelegate int Transformer(int x); static void Main(){    Transformer perform = x =>{       int result = x * x;       Console.WriteLine(result);       return result;    };    perform += x =>{       int result = x * x * x;       Console.WriteLine(result);       return result;    };    perform(2); // prints ... Read More

Explain the difference between const and readonly keywords in C#

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

784 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

404 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

568 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

554 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

678 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

332 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

Program to find out the dot product of two sparse vectors in Python

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

696 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

Program to find out the number of boxes to be put into the godown in Python

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

371 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

Program to find out if the strings supplied differ by a character in the same position in Python

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

180 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

Advertisements