Found 33676 Articles for Programming

What is the 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

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

Retrieving Elements from Collection in C#

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

856 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

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

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

String format for Double in C#

Chandu yadav
Updated on 21-Jun-2020 16:35:00

2K+ Views

Use the static method String.Format for form double string format in C#.For three decimal places −String.Format("{0:0.000}", 987.383); String.Format("{0:0.000}", 987.38); String.Format("{0:0.000}", 987.7899);For thousands separator −String.Format("{0:0,0.0}", 54567.46); String.Format("{0:0,0}", 54567.46);To format string −Exampleusing System; class Demo {    public static void Main(String[] args) {       Console.WriteLine("Three decimal places...");           Console.WriteLine( String.Format("{0:0.000}", 987.383));       Console.WriteLine( String.Format("{0:0.000}", 987.38));       Console.WriteLine(String.Format("{0:0.000}", 987.7899));           Console.WriteLine("Thousands Separator...");       Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));       Console.WriteLine(String.Format("{0:0,0}", 54567.46));    } }

Static vs. Non-Static method in C#

karthikeya Boyini
Updated on 21-Jun-2020 16:36:07

1K+ Views

Declare a member function as static. Such functions can access only static variables. The static functions exist even before the object is created.A static class cannot be instantiated and can only contain static members.Static methods is set using static keyword −public static int getNum() {    return num; }The following example demonstrates the use of static and non-static methods −Exampleusing System; namespace StaticVarApplication {    class StaticVar {       public static int num;       public void count() {          num++;       }       public static int ... Read More

Swap two variables in one line using C#

George John
Updated on 21-Jun-2020 16:13:35

587 Views

To swap two variables in a single line using the Bitwise XOR Operator.val1 = val1 ^ val2 ^ (val2 = val1);Above, we have set the values −int val1 = 30; int val2 = 60;The following is the example to swap both the variable in one line using C# −Exampleusing System; class Demo {    public static void Main(String[] args) {       int val1 = 30;       int val2 = 60;       Console.WriteLine("Values before swap");       Console.WriteLine(val1);       Console.WriteLine(val2);       val1 = val1 ^ val2 ^ (val2 = val1);       Console.WriteLine("Values after swap");       Console.WriteLine(val1);       Console.WriteLine(val2);    } }

Swap two Strings without using temp variable in C#

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; class Demo {    public static void Main(String[] args) {       String str1 = "Brad";       String str2 = "Pitt";       Console.WriteLine("Strings before swap");       Console.WriteLine(str1);       Console.WriteLine(str2);       str1 = str1 + str2; ... Read More

Tasks in C#

Ankith Reddy
Updated on 21-Jun-2020 16:14:47

3K+ Views

Task represents an asynchronous operation in C#. The following states how you can start a task in C#.Use a delegate to start a task.Task t = new Task(delegate { PrintMessage(); }); t.Start();Use Task Factory to start a task.Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });You can also use Lambda.Task t = new Task( () => PrintMessage() ); t.Start();The most basic way to start a task is using the run().Exampleusing System; using System.Threading.Tasks; public class Example {    public static void Main() {       Task task = Task.Run( () => {          int a = 0;          for (a = 0; a

Advertisements