Samual Sam has Published 2492 Articles

String Literal Vs String Object in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:34:25

1K+ Views

String LiteralsString literals or constants are enclosed in double quotes "" or with @"". A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.Here are some examples of String Literals −Hello, World" "Welcome, \The following is an example showing the usage of string ... Read More

Overriding Vs Shadowing in C#

Samual Sam

Samual Sam

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

2K+ Views

OverridingUnder overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.Let us see an example of abstract classes that implements Overriding −Exampleusing System; namespace PolymorphismApplication {    abstract class Shape {     ... Read More

Trim (Remove leading and trailing spaces) a string in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:22:39

664 Views

To trim a string in C#, use regular expression.Firstly, set the pattern for regex −string pattern = "\s+";Let’s say the following is our string with leading and trailing spaces −string input = " Welcome User ";Now using Regex, set the pattern and get the result in a new string in ... Read More

Swap two Strings without using temp variable in C#

Samual Sam

Samual Sam

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

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

Overloading in C#

Samual Sam

Samual Sam

Updated on 21-Jun-2020 16:10:33

213 Views

Overloading is of two types in C#.Function OverloadingYou can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.Let us see an example −public static int ... 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

448 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

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

744 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

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

Advertisements