Found 35164 Articles for Programming

How to use XmlSerializer in C#?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:23

431 Views

Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. To perform XML Serialization, you need the following two classes − StreamWriter class XmlSerializer class Call the Serialize method with the parameters of the StreamWriter and object to serialize. string myPath = "new.xml"; XmlSerializer s = new XmlSerializer(settings.GetType()); StreamWriter streamWriter = new StreamWriter(myPath); s.Serialize(streamWriter, settings); An XML file is visible with the name “new.xml”. Now to deserialize. MySettings mySettings = new MySettings(); string myPath = "new.xml"; XmlSerializer ... Read More

How to use Try/catch blocks in C#?

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

2K+ Views

Exceptions provide a way to transfer control from one part of a program to another. C# exception handling is built upon four keywords: try, catch, finally, and throw.try − A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.The following is an example showing how to use the try, catch, and finally in C#.Example Live Demousing ... Read More

How to use the ToString() method of array in C#?

George John
Updated on 23-Jun-2020 14:15:29

268 Views

The ToString() method returns a string that represents the current object.In the below example, we have used the ToString() method with another Array class method.arr.GetLowerBound(0).ToString()Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower {    class Program {       static void Main(string[] args) {          Array arr = Array.CreateInstance(typeof(String), 3);          arr.SetValue("One", 0);          arr.SetValue("Two", 1);          Console.WriteLine("Lower Bound {0}",arr.GetLowerBound(0).ToString());          Console.ReadLine();       }    } }OutputLower Bound 0

How to use the Compare method of the string class in C#?

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

159 Views

The Compare method compares two specified string objects and returns an integer that indicates their relative position in the sort order.Firstly, set the strings.string str1 = "Jack"; string str2 = "Mac";Now compare the strings using the Compare() method and if the comparison results 0, then it would mean the strings are equal.String.Compare(str1, str2) == 0Let us see the complete example.Example Live Demousing System; namespace StringApplication {    class StringProg {       static void Main(string[] args) {          string str1 = "Jack";          string str2 = "Mac";          if (String.Compare(str1, str2) ... Read More

How to use the Sort() method of array class in C#?

Ankith Reddy
Updated on 23-Jun-2020 14:16:54

151 Views

The Sort() method sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.Set the array.int[] list = { 22, 12, 65, 9};Use the Sort() method to sort the array.Array.Sort(list);The following is an example to learn how to work with the Sort() method.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[] list = { 22, 12, 65, 9};          Console.Write("Original Array: ");          foreach (int i in list) {         ... Read More

How to use the SetValue(,) method of array class in C#?

Samual Sam
Updated on 23-Jun-2020 14:17:33

1K+ Views

The SetValue() method sets a value to the element at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.Firstly, set the array.Array arr = Array.CreateInstance(typeof(String), 6);No set values to the element using the SetValue() method.arr.SetValue("One", 0); arr.SetValue("Two", 1); arr.SetValue("Three", 3); arr.SetValue("Four", 4); arr.SetValue("Five", 5);The following is an example showing the usage of SetValue() method in C#.Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Program {       static void Main(string[] args) {          Array arr = Array.CreateInstance(typeof(String), 6);          arr.SetValue("One", 0); ... Read More

How do you declare, initialize and access jagged arrays in C#?

Arjun Thakur
Updated on 23-Jun-2020 14:18:11

108 Views

Declare Jagged ArrayA Jagged array is an array of arrays. You can declare a jagged array named scores of type int as −int [][] points;Initialize Jagged ArrayLet us now see how to initialize it.int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, 80}, new int[]{ 60, 70 }};Access the Jagged Array ElementAccess the jagged array element as.points[i][j]);The following is the complete example showing how to work with jagged arrays in C#.Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          int[][] points = new int[][]{new ... Read More

How to use the Reverse() method of array class in C#

karthikeya Boyini
Updated on 23-Jun-2020 14:18:57

7K+ Views

The Reverse() method in array class reverses the sequence of the elements in the entire one-dimensional Array.To reverse an array, just use the Array.Reverse() method −Array.Reverse(temp);Within the reverse method, set the elements like the following code snippet.int[] list = { 29, 15, 30, 98}; int[] temp = list;You can try to run the following code to implement Reverse() method in C#.Example Live Demousing System; namespace Demo {    class MyArray {       static void Main(string[] args) {          int[] list = { 29, 15, 30, 98};          int[] temp = list;     ... Read More

How to sort one-dimensional array in ascending order using non-static method?

Chandu yadav
Updated on 23-Jun-2020 14:20:04

1K+ Views

Set the unsorted array first.int[] list = {87, 45, 56, 22, 84, 65};Now use a nested for loop to sort the list, which is passed to a function.for(int i=0; i< arr.Length; i++) {    for(int j=i+1; j=arr[j]) {          temp=arr[j];          arr[j]=arr[i];          arr[i]=temp;       }    }    Console.Write(arr[i] + " "); }The following is the complete code to sort one-dimensional array in ascending order using non-static method.Example Live Demousing System; namespace Demo {    public class MyApplication {       public static void Main(string[] args) {   ... Read More

How to use the return statement in C#?

Samual Sam
Updated on 23-Jun-2020 12:40:43

470 Views

The return statement is used to return value. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.The following is an example to learn about the usage of return statement in C#. Here, we are finding the factorial of a number and returning the result using the return statement.while (n != 1) {    res = res * n;    n = n ... Read More

Advertisements