Category: C#

C# Interfaces

Interface provides a contract among programmers, using which the programmers agree upon the name of functions, their return types and input arguments. Only the signatures of functions and properties are defined within a interface. This is different from an abstract class, where a function can be defined within body of an abstract class. In main

Simple inheritance in C#

In the following code we present a simplest example of inheritance in C#. The class BaseClass is the parent class from which a child class ChildClass is derived using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class BaseClass { public void MyMethod() { Console.WriteLine("This is base class method");

Q.No 22 Solution

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class Program { static void Main(string args) { int num = 121456890; if (((num / 100) % 10) == 1 || ((num / 100) % 10) == 0) { Console.WriteLine("Third digit is 0 or 1"); }else Console.WriteLine("Third digit is neither

Q.No 20 Solution

In below solution we used “ternary operator” which is a compact form of if-else statement. Here is the format of ternary operator: <test expression> ? (if test expression is true executed this code) : (if test expression is false execute this code) problem 19 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using

method hiding

If we use the same signature of the method in both parent and child class, and do not use virtual and override keywords respectively, then the method of the child class will “hide” the method of the parent class. To avoid this, it is always recommended to use “new” keyword with the method of the

Abstract classes

When we declare a class to be abstract, it is not possible to declare an object of the class. However, we can define a function in an abstract class (which makes it different from interface). We can also provide the signatures of methods and properties in abstract class, that must have “abstract” keyword in their

Overriding ToString() method

The ToString() method of C# is a virtually declared method. Therefore, it is possible to override the ToString() method in the inherited classes. Here is an example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { public class Money { private decimal amount; public decimal Amount { get {

Virtual methods

When a method in a class is declared as “virtual” we can “override” the method in the child class. The following is an example: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace ConsoleApplication6 { class BaseClass { public virtual void MyMethod() { Console.WriteLine("This is base class virtual method"); } }

Partial classes

A class is defined to be partial when we are working on the same class on multiple PCs. Later when the classes from different PCs are combined together, they compile to a single class. The class must have a partial keyword with it and the name of the class must be same in all PCs.

Extension methods

Extension methods are used to enhance the functionality of a class whose source code is not available. In the following example, the class Money’s functionality is extended using an extension method AddToAmount. The first argument to extension method is the class object whose functionality we want to extend and the second argument is the decimal