Found 2628 Articles for Csharp

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

457 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# 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

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

C# Program to Check status of Current Thread

Samual Sam
Updated on 19-Jun-2020 08:37:16

2K+ Views

To check the status of the current thread in C#, use the IsAlive property.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.IsAlive property to check the status of the thread −thread.IsAliveExampleLet us see the complete code to check the status of current thread in C#.Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My New Thread";          Console.WriteLine("Thread Status = {0}", thread.IsAlive);          Console.ReadKey();       }    } }OutputThread Status = True

C# Program to Convert Integer to String

karthikeya Boyini
Updated on 02-Sep-2023 15:26:43

41K+ Views

To convert an integer to string in C#, use the ToString() method.Set the integer for which you want the string −int num = 299;Use the ToString() method to convert Integer to String −String s; int num = 299; s = num.ToString();ExampleYou can try to run the following code to convert an integer to string in C# −Live Demousing System; class MyApplication {    static void Main(string[] args) {       String s;       int num = 299;       s = num.ToString();       Console.WriteLine("String = "+s);       Console.ReadLine();    } }OutputString = 299

C# program to check if there are K consecutive 1’s in a binary number

Samual Sam
Updated on 19-Jun-2020 08:37:46

207 Views

To check for consecutive 1’s in a binary number, you need to check for 0 and 1.Firstly, set a bool array for 0s and 1s i.e. false and true −bool []myArr = {false, true, false, false, false, true, true, true};For 0, set the count to 0 −if (myArr[i] == false)    count = 0;For 1, increment the count and set the result. The Max() method returns the larger of two number −count++; res = Math.Max(res, count);ExampleThe following is the example to check if there are K consecutive 1’s in a binary number −Live Demousing System; class MyApplication {    static ... Read More

C# program to check if binary representation is palindrome

Samual Sam
Updated on 19-Jun-2020 08:15:05

198 Views

To check for palindrome, let us say our number is 5, whose binary is −101The palindrome of 101 is 101 and to check you need to reverse the bits using the following function. Here, bitwise left and bitwise right shift operators are used −public static long funcReverse(long num) {    long myRev = 0;    while (num > 0) {       myRev = 1;    }    return myRev; }Then the actual representation will be compared be the reverse one by returning and getting the value from the funcReverse() function −public static bool checkPalindrome(long num) {    long ... Read More

Bitwise right shift operators in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:18:07

572 Views

Bitwise operator works on bits and performs bit by bit operation. In Bitwise right shift operator the value of the left operand is moved right by the number of bits specified by the right operand.In the below code, we have the value −60 i.e. 0011 1100On the right shift %minus;c = a >> 2;It converts into 15 after right shift twice −15 i.e. 0000 1111ExampleYou can try to run the following code to implement Bitwise right shift operator in C# −using System; using System.Collections.Generic; using System.Text; namespace Demo {    class toBinary {       static void Main(string[] args) ... Read More

Advertisements