Found 34494 Articles for Programming

Managed code vs Unmanaged code in C#

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

3K+ 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

3K+ 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

10K+ 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

813 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

298 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

589 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

199 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# using %

Ankith Reddy
Updated on 22-Jun-2020 13:07:13

244 Views

Use the String.Fornt to format a string using %.String.Format format controls in C# also includes percentage (%) This multiplies the value by 100 and appends a percentage sign.Let’s say our value is −double val = .322;Now, use String.Format and format −string.Format("string = {0:0.0%}", val);The following is an example −Example Live Demousing System; public class Program {    public static void Main() {       double val = .322;       string res = string.Format("string = {0:0.0%}", val);       Console.WriteLine(res);    } }Outputstring = 32.2%

String Formatting in C# to add padding

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

799 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

Advertisements