Found 2745 Articles for Csharp

C# Example for MultiLevel Inheritance

Samual Sam
Updated on 19-Jun-2020 08:58:22

4K+ Views

Multilevel Inheritance occurs when a derived class is formed from another derived class.Grandfather, father, and son are the perfect example to represent Multilevel Inheritance in C# −ExampleThe following is an example stating the usage of multilevel inheritance in C#.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Son : Father {       public void DisplayTwo() {          Console.WriteLine("Son.. ");       }       static void Main(string[] args) {          Son s = new Son();          s.Display();          s.DisplayOne(); ... Read More

C# Example for Single Inheritance

karthikeya Boyini
Updated on 19-Jun-2020 08:58:59

3K+ Views

The following is an example of Single Inheritance in C#. In the example, the base class is Father and declared like the following code snippet −class Father {    public void Display() {       Console.WriteLine("Display");    } }Our derived class is Son and is declared below −class Son : Father {    public void DisplayOne() {       Console.WriteLine("DisplayOne");    } }ExampleThe following is the complete example to implement Single Inheritance in C#.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyAplication {    class Demo {       static void Main(string[] args) {   ... Read More

C# Bitwise and Bit Shift Operators

Samual Sam
Updated on 19-Jun-2020 09:00:12

3K+ Views

Bitwise operator works on bits and performs bit by bit operation.The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13 −OperatorDescriptionExample&Bitwise AND Operator copies a bit to the result if it exists in both operands.(A & B) = 12, which is 0000 1100|Bitwise OR Operator copies a bit if it exists in either operand.(A | B) = 61, which is 0011 1101^Bitwise XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) = 49, which is 0011 0001~Bitwise One's Complement Operator ... Read More

C# program to check if two matrices are identical

karthikeya Boyini
Updated on 19-Jun-2020 08:27:53

396 Views

To check whether the matrices are identical or not, you need to first check whether the matrixes can be compared or not, since for comparison at least the dimensions of the two matrices should be the same.if (row1 != row2 && col1 != col2) {    Console.Write("Matrices can't be compared:"); }Now, under else condition check for whether the metrics are identical or not. We have also set a flag here −if (row1 != row2 && col1 != col2) {    Console.Write("Matrices can't be compared:"); } else {    Console.Write("Comparison of Matrices: ");    for (i = 0; i < row1; ... Read More

C# program to check if string is panagram or not

Samual Sam
Updated on 19-Jun-2020 08:28:49

1K+ Views

A pangram has all the 26 letters of an alphabet.Below, we have entered a string, and will check whether it is a pangram or not −string str = "The quick brown fox jumps over the lazy dog";Now, check using the ToLower(), isLetter() and Count() functions that the string has all the 26 letters of not since pangram has all the 26 letters of an alphabet.ExampleYou can try to run the following code to check whether a string is pangram or not.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo {    public class Program {       ... Read More

C# program to check if a Substring is present in a Given String

karthikeya Boyini
Updated on 19-Jun-2020 08:30:11

442 Views

Use the contains() method in C# to check if a substring is in a given string.Let us say the string is −UnitedWithin the string, you need to find the substring “Uni”. For that, use the contains method and use it like the following code snippet −res = str1.Contains(str2);ExampleYou can try to run the following code to find a substring in a string.Live Demousing System; public class Demo {    public static void Main() {       string str1 = "United", str2 = "Uni";       bool res;       res = str1.Contains(str2);       if (res) ... Read More

C# Example for Hierarchical Inheritance

Samual Sam
Updated on 19-Jun-2020 08:32:41

3K+ Views

More than one class is inherited from the base class in Hierarchical Inheritance.In the example, our base class is Father −class Father {    public void display() {       Console.WriteLine("Display...");    } }It has Son and Daughter as the derived class. Let us how to add a derived class in Inheritance −class Son : Father {    public void displayOne() {       Console.WriteLine("Display One");    } }ExampleThe following the complete example of implementing Hierarchical Inheritance in C# −using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Inheritance {    class Test {       static ... Read More

C# Anonymous Methods

karthikeya Boyini
Updated on 19-Jun-2020 08:34:32

111 Views

Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are the methods without a name, just the body.Let us see how to declare Anonymous methods in C# −delegate void NumberChanger(int n); ... NumberChanger nc = delegate(int x) {    Console.WriteLine("Anonymous Method: {0}", x); };ExampleThe following is an example to implement Anonymous methods in C#.Live Demousing System; delegate void NumberChanger(int n); namespace DelegateAppl {    class Demo {       static int num = 10;       public static void AddNum(int p) {          num += p;     ... Read More

C# and Multiple Inheritance

Samual Sam
Updated on 19-Jun-2020 08:35:22

5K+ Views

Multiple Inheritance isn’t supported in C#. To implement multiple inheritances, use Interfaces.Here is our interface PaintCost in class Shape −public interface PaintCost {    int getCost(int area); }The shape is our base class whereas Rectangle is the derived class −class Rectangle : Shape, PaintCost {    public int getArea() {       return (width * height);    }    public int getCost(int area) {       return area * 80;    } }Let us now see the complete code to implement Interfaces for multiple inheritances in C# −Using System; namespace MyInheritance {    class Shape {     ... Read More

Bubble Sort program in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:36:06

14K+ Views

Bubble sort is a simple sorting algorithm. This sorting algorithm is a comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.Let’s say our int has 5 elements −int[] arr = { 78, 55, 45, 98, 13 };Now, let us perform Bubble Sort.Start with the first two elements 78 and 55. 55 is smaller than 78, so swap both of them. Now the list is −55, 78, 45, 98, 13Now 45 is less than 78, so swap it.55, 45, 78, 98, 3Now 98 is greater than 78, so ... Read More

Advertisements