Server Side Programming Articles - Page 2468 of 2650

Managed code vs Unmanaged code in C#

Prabhas
Updated on 22-Jun-2020 13:15:33

4K+ Views

Unmanaged CodeApplications that are not under the control of the CLR are unmanagedThe unsafe code or the unmanaged code is a code block that uses a pointer variable.The unsafe modifier allows pointer usage in unmanaged code.Let us see the example −Examplestatic unsafe void Main(string[] args) {    int var = 20;    int* p = &var;    Console.WriteLine("Data is: {0} ", var);    Console.WriteLine("Address is: {0}", (int)p);    Console.ReadKey(); }Managed CodeManaged code is a code whose execution is managed by Common Language Runtime. It gets the managed code and compiles it into machine code. After that, the code is executed.The ... Read More

Matching strings with a wildcard in C#

varma
Updated on 22-Jun-2020 13:15:59

5K+ Views

Commonly used wildcard characters are the asterisk (*). It represents zero or more characters in a string of characters.In the following example asterisk is used to match words that begins with m and ends with e −@”\bt\S*s\b”The following is the complete code −Example Live Demousing System; using System.Text.RegularExpressions; namespace Demo {    public class Program {       private static void showMatch(string text, string expr) {          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);          }     ... Read More

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

369 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

723 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

289 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

484 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

Advertisements