Found 2628 Articles for Csharp

How to add an item to an ArrayList in C#?

Samual Sam
Updated on 21-Jun-2020 15:51:25

141 Views

ArrayList is a non-generic type of collection in C# that dynamically resizes.Let us see how to initialize ArrayList in C# −ArrayList arr= new ArrayList();Add an item to an Array List −ArrayList arr1 = new ArrayList(); arr1.Add(30); arr1.Add(70);Let us see the complete example to implement ArrayList in C#. Here we have two array lists. The 2nd array list is appended to the first list.Exampleusing System; using System.Collections; public class MyClass {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add(30);       arr1.Add(70);       ArrayList arr2 = ... Read More

Initializing HashSet in C#

Ankith Reddy
Updated on 21-Jun-2020 15:50:18

608 Views

To initialize a HashSet.var h = new HashSet(arr1);Above, we have set an array in the HashSet. The following is the array −string[] arr1 = {    "electronics",    "accessories”,    "electronics", };The following is an example showing how to implement HashSet in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       string[] arr1 = {          "electronics",          "accessories”,          "electronics",       };       Console.WriteLine(string.Join(",", arr1));       // HashSet       var h = new HashSet(arr1);       // eliminates duplicate words       string[] arr2 = h.ToArray();       Console.WriteLine(string.Join(",", arr2));    } }

c# Put spaces between words starting with capital letters

Arjun Thakur
Updated on 21-Jun-2020 15:52:44

3K+ Views

To place spaces in between words starting with capital letters, try the following example −Firstly, set the string.var str = "WelcomeToMyWebsite";As you can see above, our string isn’t having spaces before capital letter. To add it, use LINQ as we have done below −str = string.Concat(str.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');The following is the complete code to place spaces between words beginning with capital letters −Exampleusing System; using System.Linq; class Demo {    static void Main() {       var str = "WelcomeToMyWebsite";       Console.WriteLine("Original String: "+str);     ... Read More

Reverse words in a given String in C#

Chandu yadav
Updated on 21-Jun-2020 15:53:26

511 Views

Let’s say the following is the string −WelcomeAfter reversing the string, the words should be visible like −emocleWUse the reverse() method and try the following code to reverse words in a string −Exampleusing System; using System.Linq; class Demo {    static void Main() {       string str = "Welcome";       // reverse the string       string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));       Console.WriteLine(res);    } }

String format for DateTime in C#

Samual Sam
Updated on 21-Jun-2020 15:54:17

336 Views

Format DateTime using String.Format method.Let us see an example −Exampleusing System; static class Demo {    static void Main() {       DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);       Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));       Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));       Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));    } }Above we have first set the DateTime class object −DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);To format, we have used the String.Format() method and displayed date in different formats.String.Format("{0:y yy yyy yyyy}", d) String.Format("{0:M MM MMM MMMM}", d) String.Format("{0:d dd ddd dddd}", d

Serialization and Deserialization in C#

karthikeya Boyini
Updated on 21-Jun-2020 15:54:49

1K+ Views

Serialization converts objects into a byte stream and brings it to a form that it can be written on stream. This is done to save it to memory, file or database.Serialization can be performed as −Binary SerializationAll the members, even members that are read-only, are serializedXML SerializationIt serializes the public fields and properties of an object into XML stream conforming to a specific XML Schema definition language document.Let us see an example. Firstly set the stream −FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter();Now create an object of the class and call the constructor which has three parameters −Employee ... Read More

Reverse a string in C#

Chandu yadav
Updated on 21-Jun-2020 15:54:35

436 Views

To reverse a string, use the Array. Reverse() method.We have set a method and passed the string value as “Henry” −public static string ReverseFunc(string str) {    char[] ch = str.ToCharArray();    Array.Reverse(ch);    return new string(ch); }In the above method, we have converted the string into character array −char[] ch = str.ToCharArray();Then the Reverse() method is used −Array.Reverse(ch);

Streams and Byte Streams in C#

Samual Sam
Updated on 21-Jun-2020 15:55:37

1K+ Views

A file is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a stream.The type of streams includes −Byte Streams − It includes Stream, FileStream, MemoryStream and BufferedStream.Character Streams − It includes Textreader-TextWriter, StreamReader, StraemWriter and other streams.Byte streams have classes that consider data in the stream as byte.Stream class is the base for other byte stream classes. The following are the properties −CanRead − Whether stream supports readingCanWrite − Whether stream supports writingLength − Length of the streamThe System.IO namespace has ... Read More

What are the differences between a dictionary and an array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 15:36:19

551 Views

DictionaryDictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To declare a Dictionary −IDictionary d = new Dictionary();To add elements −IDictionary d = new Dictionary(); d.Add(1,97); d.Add(2,89); d.Add(3,77); d.Add(4,88);ArrayArray stores a fixed-size sequential collection of elements of the same type. It consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.To define Arrays −int[] arr = new int[5]; To initialize and set elements to Arrays.int[] arr = new int[10] {3, 5, 35, 87, 56, 99, 44, 36, 78};

final, finally and finalize in C#

George John
Updated on 21-Jun-2020 15:36:34

3K+ Views

finalJava has final keyword, but C# does not have its implementation. For the same implementation, use the sealed keyword.With sealed, you can prevent overriding of a method. When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.FinallyThe finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.FinalizeThe ... Read More

Advertisements