In C#, you can use the comma to declare more than one local variable in a statement. The following displays the same −int a = 20, b = 70, c = 40, d = 90;ExampleLet us see an example in which we are declaring multiple local variables. Below four variable is declared and initialized in the same statement.Live Demousing System; class Demo { static void Main() { int a = 20, b = 70, c = 40, d = 90; Console.WriteLine("{0} {1} {2} {3}", a, b, c, d); } }Output20 70 40 90
Use the StartWith() method in C# to check for URL in a String.Let us say our input string is −string input = "https://example.com/new.html";Now we need to check for www or non-www link. For this, use the if statement in C# −if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) { }ExampleYou can try to run the following code to check for URL in a string.Live Demousing System; class Demo { static void Main() { string input = "https://example.com/new.html"; // See if input matches one of these starts. if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) { ... Read More
To get the current time in a millisecond, use the date getMilliseconds() method. JavaScript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.Example You can try to run the following code to get the current time in milliseconds − JavaScript getMilliseconds() Method var dt = new Date( ); document.write("getMilliseconds() : " + dt.getMilliseconds() );
To calculate the area and perimeter of a circle, you can try to run the following code −Example JavaScript Example function Calculate(r) { this.r = r; this.perimeter = function () { return 2*Math.PI*this.r; }; this.area = function () { return Math.PI * this.r * this.r; }; } var calc = new Calculate(5); document.write("Area of Circle = ", calc.area().toFixed(2)); document.write("Perimeter of Circle = ", calc.perimeter().toFixed(2));
A regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on the text.A regular expression could be defined with the RegExp () constructor, as follows −var pattern = new RegExp(pattern, attributes); or simply var pattern = /pattern/attributes;The following are the parameters −pattern − A string that specifies the pattern of the regular expression or another regular expression.attributes − An optional string containing any of the "g", "i", and "m" attributes that specify global, ... Read More
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP