Use Continue Statement in While Loop in C#

Samual Sam
Updated on 23-Jun-2020 14:31:50

277 Views

The continue statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.The continue statement in C# works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.For the while loop, continue statement causes the program control passes to the conditional tests.The following is the complete code to use continue statement in a while loop.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {         ... Read More

Prevent Object of a Class from Garbage Collection in Java

Ankith Reddy
Updated on 23-Jun-2020 14:30:45

299 Views

If a object is no more referenced by a live reference then it becomes eligible for garbage collection. See the example below −Examplepublic class Tester{    public static void main(String[] args) {       test();    }    public static void test(){       A a = new A();    } } class A {}When test() method complete execution, the a object is no more referenced and is eligible for garbage collection. Java garbage collector will deallocate the object when it runs.To prevent garbage collection, we can create a static reference to an object and then ... Read More

Use foreach Statement to Loop Through Array Elements in C#

Samual Sam
Updated on 23-Jun-2020 14:28:39

376 Views

A foreach loop is used to execute a statement or a group of statements for each element in an array or collection.It is similar to for Loop; however, the loop is executed for each element in an array or group. Therefoe, the index does not exist in it.Let us see an example of Bubble Sort, wherein after sorting the elements, we will display the elements using the foreach loop.Example Live Demousing System; namespace BubbleSort {    class MySort {       static void Main(string[] args) {          int[] arr = { 78, 55, 45, 98, 13 };          int temp;          for (int j = 0; j

Prevent Cloning to Break Singleton Class Pattern

George John
Updated on 23-Jun-2020 14:27:50

2K+ Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonpublic class Tester{    public static void main(String[] args)    throws CloneNotSupportedException {       A a = A.getInstance();       A b = (A)a.clone();       System.out.println(a.hashCode());       System.out.println(b.hashCode());    } } ... Read More

Sort List of Dictionaries by Values in C#

Samual Sam
Updated on 23-Jun-2020 14:26:29

500 Views

Set the list of dictionaries with keys and values.var d = new Dictionary(); d.Add("Zack", 0); d.Add("Akon", 3); d.Add("Jack", 2); d.Add("Tom", 1);Get and sort the keys.var val = d.Keys.ToList(); val.Sort();You can try to run the following code to sort a list of dictionaries by values.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       var d = new Dictionary();       d.Add("Zack", 0);       d.Add("Akon", 3);       d.Add("Jack", 2);       d.Add("Tom", 1);       // Acquire keys and sort them.       var val ... Read More

Split String into Elements of a String Array in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:25:16

1K+ Views

Set the string you want to split.string str = "Hello World!";Use the split() method to split the string into separate elements.string[] res = str.Split(' ');The following is the complete code to split a string into elements of a string array in C#.Example Live Demousing System; class Demo {    static void Main() {       string str = "Hello World!";       string[] res = str.Split(' ');       Console.WriteLine("Separate elements:");       foreach (string words in res) {          Console.WriteLine(words);       }    } }OutputSeparate elements: Hello World!

Split a String Using Regular Expressions in C#

George John
Updated on 23-Jun-2020 14:24:54

1K+ Views

To split a string suing regular expression, use the Regex.split.Let’s say our string is −string str = "Hello\rWorld";Now use Regex.split to split the string as shown below −tring[] res = Regex.Split(str, "\r");The following is the complete code to split a string using Regular Expression in C#.Example Live Demousing System; using System.Text.RegularExpressions; class Demo {    static void Main() {       string str = "Hello\rWorld";       string[] res = Regex.Split(str, "\r");       foreach (string word in res) {          Console.WriteLine(word);       }    } }OutputHello World

Split String with a Delimiter in C#

Samual Sam
Updated on 23-Jun-2020 14:24:32

890 Views

Delimiters are the commas that you can see in the below string.string str = "Welcome, to, New York";Now set the delimiter separately.char[] newDelimiter = new char[] { ', ' };Use theSplit() method to split the string considering the delimiter as the parameter.str.Split(newDelimiter, StringSplitOptions.None);To split a string with a string deli meter, try to run the following code −Example Live Demousing System; class Program {    static void Main() {       string str = "Welcome, to, New York";       char[] newDelimiter = new char[] { ', ' };       string[] arr = str.Split(newDelimiter, StringSplitOptions.None);     ... Read More

Access Elements from Two-Dimensional Array in C#

Chandu yadav
Updated on 23-Jun-2020 14:23:36

4K+ Views

A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.An element in 2-dimensional array is accessed by using the subscripts. That is, row index and column index of the array.int x = a[1, 1]; Console.WriteLine(x);Let us see an example that shows how to access elements from two-dimensional array.Example Live Demousing System; namespace Demo {    class MyArray {       static void Main(string[] args) {          /* an array with 5 rows and 2 columns*/          int[, ] a = new int[5, 2] ... Read More

Call a Hash Method Recursively

Samual Sam
Updated on 23-Jun-2020 14:21:46

330 Views

To call a C# method recursively, you can try to run the following code. Here, Factorial of a number is what we are finding using a recursive function display().If the value is 1, it returns 1 since Factorial is 1.if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if 1you want the value of 5!Interation1: 5 * display(5 - 1); Interation2: 4 * display(4 - 1); Interation3: 3 * display(3 - 1); Interation4: 4 * display(2 - 1);The following is the complete code to call a C# method recursively.Example Live Demousing System; ... Read More

Advertisements