String Literal vs String Object in C#

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 literals −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          // string          string str1 ="Hello, World";          Console.WriteLine(str1);          // Multi-line string   ... Read More

Return Keyword in C#

Arjun Thakur
Updated on 21-Jun-2020 16:32:10

3K+ Views

The return statement is used to return value. When a program calls a function, the program control is transferred to the called function.The following is an example to learn about the usage of return statement in C#. Here, we are finding the average and returning the result using the return statement.double getAverage(int[] arr, int size) {    int i;    double avg;    int sum = 0;    for (i = 0; i < size; ++i) {       sum += arr[i];    }    avg = (double)sum / size;    return avg; }Here is the complete ... Read More

Retrieve Elements from Collection in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:31:09

873 Views

Let us see an example of a list collection.We have set the elements −List list = new List(); list.Add(20); list.Add(40); list.Add(60); list.Add(80);Now let’s say we need to retrieve the first element from the list. For that, set the index like this −int a = list[0];The following is an example showing how to retrieve elements from a list collection −Exampleusing System; using System.Collections.Generic; class Demo {    static void Main(String[] args) {       List list = new List();       list.Add(20);       list.Add(40);       list.Add(60);       list.Add(80);     ... Read More

Overriding vs Shadowing in C#

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 {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;       ... Read More

Difference Between Overriding and Shadowing in C#

George John
Updated on 21-Jun-2020 16:28:20

2K+ Views

The following are the differences between overriding and shadowing −Shadowing redefines the complete method, whereas overriding redefines only the implementation of the method.In Overriding, you can access the base class using the child class’ object overridden method.. Shadowing has cannot access the chaild class methos.Shadowing is also known as method hiding. The method of the parent class is available to the child class without using the override keyword in shadowing. The child class has its own version of the same function.Under overriding, you can define a behavior that is specific to the subclass type, which means a subclass can implement ... Read More

Virtual vs Sealed vs New vs Abstract in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:28:08

4K+ Views

VirtualThe virtual keyword allows a class to be overridden. For overriding a parent class method in the child class, declare the parent class method as virtual.SealedWhen a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.To prevent being overridden, use the sealed in C#. 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.public sealed override void getResult() { }NewUse the new keyword to hide the base class method ... Read More

Swap Two Numbers in C#

Ankith Reddy
Updated on 21-Jun-2020 16:27:42

1K+ Views

To swap two numbers, work with the following logic.Set two variables for swapping −val1 = 100; val2 = 200;Now perform the following operation for swap −val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;The following is the code −Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int val1,val2;          val1 = 100;          val2 = 200;          Console.WriteLine("Values before swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          val1 = val1 + val2;          val2 = val1 - val2;          val1 = val1 - val2;              Console.WriteLine("Values after swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          Console.ReadLine();       }    } }

Private Methods in C#

Chandu yadav
Updated on 21-Jun-2020 16:26:34

8K+ Views

Private Methods can only be used inside the class. To set private methods, use the private access specifier.Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.The following is an example −Exampleusing System; class Demo {    private int displayOne() {       return 10;    }    public int displayTwo() {       return 10;    } } class Program {    static ... Read More

Networking in C#

George John
Updated on 21-Jun-2020 16:25:00

3K+ Views

The .NET Framework has a layered, extensible, and managed implementation of networking services. You can easily integrate them into your applications. Use the System.Net; namespace.Let us see how to acess the Uri class:.In C#, it provides object representation of a uniform resource identifier (URI) −Uri uri = new Uri("http://www.example.com/"); WebRequest w = WebRequest.Create(uri);Let us now see the System.Net class. This is used to encorypt connections using using the Secure Socket Layer (SSL). If the URI begins with "https:", SSL is used; if the URI begins with "http:", an unencrypted connection is used.The following is an example. For SSL with FTP, ... Read More

Swap Two Numbers Without Using a Temp Variable in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:24:18

838 Views

To swap two numbers, use the third variable and perform arithmetical operator without using a temp variable.Set two variables for swapping −val1 = 5; val2 = 10;Now perform the following operation for swap −val1 = val1 + val2; val2 = val1 - val2; val1 = val1 - val2;Exampleusing System; namespace Demo {    class Program {       static void Main(string[] args) {          int val1,val2;          val1 = 5;          val2 = 10;          Console.WriteLine("Values before swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          val1 = val1 + val2;          val2 = val1 - val2;          val1 = val1 - val2;          Console.WriteLine("Values after swap...");          Console.WriteLine(val1.ToString());          Console.WriteLine(val2.ToString());          Console.ReadLine();       }    } }

Advertisements