Samual Sam has Published 2310 Articles

Swap two Strings without using temp variable in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:14:17

2K+ Views

To swap two strings without using a temp variable, you can try the following code and logic.Append the second string with the first.str1 = str1 + str2;Set the str1 in str2.str2 = str1.Substring(0, str1.Length - str2.Length);Now, the final step is to set str2 in str1 −str1 = str1.Substring(str2.Length);Exampleusing System; ... Read More

Null Pointer Exception in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:08:32

3K+ Views

NullReferenceException is a C# version of NullPointerException. To handle and catch it in C#, use try-catch.The below example shows that a variable is set to null and when we try to print it, it throws an exception that gets caught in the catch −Try {    a = null;   ... Read More

How to reverse a String using C#?

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:05:29

591 Views

To reverse a string, use the Array.Reverse() method.Set the string you want to reverse −string str = "Amit";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);Exampleusing System; namespace Demo {    class Program {       static ... Read More

Print Single and Multiple variable in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:03:23

9K+ Views

To display single variable value in C#, you just need to use Console.WriteLine()Let us see an example. Here, we have displayed the value of a single variable “a” in a line −Exampleusing System; using System.Linq; class Program {    static void Main() {       int a = ... Read More

Private Constructors and Singleton Classes in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:00:43

987 Views

A private constructor is used in classes containing only static member as shown below −class Demo {    // private constructor    private Demo() { }    public static a = 10; }A singleton class has normal methods and you can call it using an instance.To prevent multiple instances ... Read More

Streams and Byte Streams in C#

Samual Sam

Samual Sam

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

2K+ 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 ... Read More

String format for DateTime in C#

Samual Sam

Samual Sam

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

458 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 ... Read More

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

Samual Sam

Samual Sam

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

217 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 ... Read More

Final variables in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 15:47:04

5K+ Views

Java has a final keyword, but C# does not have its implementation. Use the sealed or readonly keyword in C# for the same implementation.The readonly would allow the variables to be assigned a value only once. A field marked "read-only", can only be set once during the construction of an ... Read More

What are the differences between a class and struct in C#?

Samual Sam

Samual Sam

Updated on 21-Jun-2020 15:36:52

658 Views

ClassClass is a blueprint for a data type. A class definition starts with the keyword class followed by the class name.StructA structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating ... Read More

Advertisements