Found 2587 Articles for Csharp

Exit Methods in C# Application

radhakrishna
Updated on 22-Jun-2020 13:16:20

12K+ Views

Environment.Exit() methodThe Environment.Exit() method terminates the process and returns an exit code to the operating system −Environment.Exit(exitCode);Use exitCode as 0 (zero) to show that the process completed successfully.Use exitCode as a non-zero number to show an error, for example −Environment.Exit(1) − Return a value 1 to show that the file you want is not presentEnvironment.Exit(2) − Return a value 2 to indicate that the file is in an incorrect format.System.Windows.Forms.Application.ExitThread( )To close a subapplication or current thread in a Windows Form, use theSystem.Windows.Forms.Application.ExitThread( ).private void buttonClose_Click(object sender, EventArgs eventArgs) {    System.Windows.Forms.Application.ExitThread( ); }Read More

How do you convert a list collection into an array in C#?

vanithasree
Updated on 22-Jun-2020 13:05:09

1K+ Views

Firstly, set a list collection −List < string > myList = new List < string > (); myList.Add("RedHat"); myList.Add("Ubuntu");Now, use ToArray() to convert the list to an array −string[] str = myList.ToArray();The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("RedHat");       myList.Add("Ubuntu");       Console.WriteLine("List...");       foreach(string value in myList) {          Console.WriteLine(value);       } ... Read More

How to set a value to the element at the specified position in the one-dimensional array in C#

radhakrishna
Updated on 22-Jun-2020 13:05:58

363 Views

Firstly, set the array −int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};Now, let us say you need to set an element at position 1 −p[2] = 77;Let us see the comple code −Example Live Demousing System; namespace Program {    public class Demo {       public static void Main(string[] args) {          int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};          int j;          Console.WriteLine("Initial Array......");          for (j = 0; j < p.Length; j++ ) { ... Read More

Swapping Characters of a String in C#

Arjun Thakur
Updated on 22-Jun-2020 13:06:49

2K+ Views

To swap characters of a string, use the Select method.Firstly, let’s say our string is −string str = "PQRQP";Now, you need to swap every occurrence of P with Q and Q with P −str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();The above replaces the characters.Let us see the compelet code −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       string str = "PQRQP";       var res= str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();       str = new String(res);       Console.WriteLine(str);    } }OutputQPRPQ

String Formatting with ToString in C#

varun
Updated on 22-Jun-2020 13:06:18

706 Views

To format a string, first set the value −int value = 55;Now to format the integer, use ToString and let’s say we need to set it for three places −value.ToString("000");The following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       int value = 55;       string res = value.ToString("000");       Console.WriteLine(res);    } }Output055

String Formatting in C# to add padding on the right

vanithasree
Updated on 22-Jun-2020 13:07:37

280 Views

To add padding on the right to a string −const string format = "{0,10}";Now add it to the string −string str1 = string.Format(format, "Marks","Subject");Let us see the complete code −Example Live Demousing System; public class Program {    public static void Main() {       // set right padding       const string format = "{0,10}";       string str1 = string.Format(format, "Marks","Subject");       string str2 = string.Format(format, "95","Maths");       Console.WriteLine(str1);       Console.WriteLine(str2);    } }OutputMarks 95

String Formatting in C# to add padding

seetha
Updated on 22-Jun-2020 13:08:09

1K+ Views

With C#, you can easily format content and add padding to it.To add padding −const string format = "{0,-5} {1,5}";Now, add the padding to the strings −string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom");Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       // set padding       const string format = "{0,-5} {1,5}";       string str1 = string.Format(format, "Rank","Student");       string str2 = string.Format(format, "2","Tom");       Console.WriteLine(str1);       Console.WriteLine(str2);    } }OutputRank Student 2 Tom

String slicing in C# to rotate a string

George John
Updated on 22-Jun-2020 13:08:40

462 Views

Let’s say our string is −var str = "welcome";Use the substring() method and the following, if you want to rotate only some characters. Here, we are rotating only 2 characters −var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);The following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       var str = "welcome";       Console.WriteLine("Original String = "+str);       var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);       Console.WriteLine("Rotating two characters in the String: "+res);    } }OutputOriginal String = welcome Rotating two characters in the String: elcomewe

Merge two sorted arrays in C#

radhakrishna
Updated on 22-Jun-2020 13:09:18

837 Views

To merge two sorted arrays, firstly set two sorted arrays −int[] array1 = { 1, 2 }; int[] array2 = { 3, 4 };Now, add it to a list and merge −var list = new List(); for (int i = 0; i < array1.Length; i++) {    list.Add(array1[i]);    list.Add(array2[i]); }Use the ToArray() method to convert back into an array −int[] array3 = list.ToArray();The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       int[] array1 = { 1, 2 };       int[] array2 ... Read More

UnionWith Method in C#

Chandu yadav
Updated on 22-Jun-2020 13:09:51

685 Views

Use the UnionWith method in C# to get the union of i.e. unique lements from two collections.Let’s say the following are our Dictionary −Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("pencil", 1); dict1.Add("pen", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("pen", 3);Now, use HashSet and UnionWith to get the union −HashSet < string > hSet = new HashSet < string > (dict1.Keys); hSet.UnionWith(dict2.Keys);The following is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {   ... Read More

Advertisements