Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Chandu yadav
Page 7 of 81
C# Program to return a collection with repeated elements
To return a collection with repeated elements in C#, use Enumerable.Repeat method.It is part of System.Linq namespace.Let’s say you need to repeat a number twice, for that set the number and the frequency of repetition.Enumerable.Repeat(50, 2);Now assign it to a variable and display it.Exampleusing System; using System.Linq; class Demo { static void Main() { // repeating element 50, two times var num = Enumerable.Repeat(50, 2); // displayig repeating elements foreach (int ele in num) { Console.WriteLine(ele); } } }Output50 50
Read MoreHow to format message with integer fillers in Java
To format message with integer fillers in Java, we use the MessageFormat class. The MessageFormat class gives us a way to produce concatenated messages which are not dependent on the language. The MessageFormat class extends the Serializable and Cloneable interfaces.Declaration − The java.text.MessageFormat class is declared as follows −public class MessageFormat extends FormatThe MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in {} curly ...
Read MoreLinkedList AddAfter method in C#
Set a LinkedList.int [] num = {1, 2, 3, 4, 5}; LinkedList list = new LinkedList(num);Now add a node at the end using AddLast() method.var newNode = list.AddLast(20);To add a node after the above added node, use the AddAfter() method.list.AddAfter(newNode, 30);Exampleusing System; using System.Collections.Generic; class Demo { static void Main() { int [] num = {1, 2, 3, 4, 5}; LinkedList list = new LinkedList(num); foreach (var n in list) { Console.WriteLine(n); } // adding a node at the end ...
Read MoreSelection Sort program in C#
Selection Sort is a sorting algorithm that finds the minimum value in the array for each iteration of the loop. Then this minimum value is swapped with the current array element. This procedure is followed until the array is sorted.A program that demonstrates selection sort in C# is given as follows.Exampleusing System; public class Example { static void Main(string[] args) { int[] arr = new int[10] { 56, 1, 99, 67, 89, 23, 44, 12, 78, 34 }; int n = 10; Console.WriteLine("Selection sort"); Console.Write("Initial array is: ...
Read MoreHow to create a thread in C#?
Threads are lightweight processes. A thread is defined as the execution path of a program. Threads are created by extending the Thread class. The extended Thread class then calls the Start() method to begin the child thread execution.Example of Thread: One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.The following is an example showing how to create a thread.Exampleusing System; using System.Threading; namespace Demo { class Program { public static void ThreadFunc() { ...
Read MoreC# Program to find the longest string from an array of strings using Lambda Expression
The following is our string array −string[] arr = { "Java", "HTML", "CSS", "JavaScript"};Use the Aggregate method and set a Lambda Expression to find the string with more number of characters.Here, the resultant string should have more number of characters than the initial seed value i.e. “jQuery” here.Exampleusing System; using System.Linq; class Demo { static void Main() { string[] arr = { "Java", "HTML", "CSS", "JavaScript"}; string res = arr.AsQueryable().Aggregate("jQuery", (longest, next) => next.Length > longest.Length ? next : longest,str => str.ToLower()); Console.WriteLine("String with more number of characters: {0}", res); } }OutputString with more number of characters: javascript
Read MoreC# program to multiply two matrices
The program for matrix multiplication is used to multiply two matrices. This procedure is only possible if the number of columns in the first matrix are equal to the number of rows in the second matrix.A program that demonstrates matrix multiplication in C# is given as follows −Exampleusing System; namespace MatrixMultiplicationDemo { class Example { static void Main(string[] args) { int m = 2, n = 3, p = 3, q = 3, i, j; int[, ] a = {{1, 4, 2}, {2, 5, 1}}; ...
Read MoreCount the Number of matching characters in a pair of Java string
In order to find the count of matching characters in two Java strings the approach is to first create character arrays of both the strings which make comparison simple.After this put each unique character into a Hash map.Compare each character of other string with created hash map whether it is present or not in case if present than put that character into other hash map this is to prevent duplicates.In last get the size of this new created target hash map which is equal to the count of number of matching characters in two given strings.Exampleimport java.util.HashMap; public class MatchingCharacters ...
Read MoreHow to negate the positive elements of an integer array in C#?
The following is the array and its elements −int[] arr = { 10, 20, 15 };Set negative value to positive elements.if (arr[i] > 0) arr[i] = -arr[i];Loop the above until the length of the array.for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); if (arr[i] > 0) arr[i] = -arr[i]; }Let us see the complete example.Exampleusing System; public class Demo { public static void Main(string[] args) { int[] arr = { 10, 20, 15 }; Console.WriteLine("Displaying elements..."); for (int i = 0; i < arr.Length; ...
Read MoreWhat are Base and Derived Classes in C#?
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.For example, Vehicle Base class with the following Derived Classes.Truck Bus MotobikeThe derived class inherits the base class member variables and member methods.In the same way, the derived class for Shape class can be Rectangle as in the following example.Exampleusing System; namespace Program { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) ...
Read More