Csharp Articles

Page 148 of 196

Retrieving Elements from Collection in C#

karthikeya Boyini
karthikeya Boyini
Updated on 21-Jun-2020 937 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
Samual Sam
Updated on 21-Jun-2020 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

What is the difference between overriding and shadowing in C#?

George John
George John
Updated on 21-Jun-2020 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
karthikeya Boyini
Updated on 21-Jun-2020 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

Private Methods in C#

Chandu yadav
Chandu yadav
Updated on 21-Jun-2020 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
George John
Updated on 21-Jun-2020 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

How to swap two numbers without using a temp variable in C#

karthikeya Boyini
karthikeya Boyini
Updated on 21-Jun-2020 906 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();       }    } }

Read More

try keyword in C#

Arjun Thakur
Arjun Thakur
Updated on 21-Jun-2020 352 Views

A try block identifies a block of code for which particular exceptions is activated. It is followed by one or more catch blocks.try { }With that, you need to set catch statement as well to catch the exception −try {       // statements causing exception    } catch( ExceptionName e1 ) {       // error handling code    }The following is an example −Exampleclass Demo {    int result;    Demo() {       result = 0;    }    public void division(int val1, int val2) {       try {   ...

Read More

Timer in C#

karthikeya Boyini
karthikeya Boyini
Updated on 21-Jun-2020 3K+ Views

The namespace used to set a timer is System. Timers. The Timer class generates an event after a set interval, with an option to generate recurring events.Firstly, create a timer object for 5 seconds interval −timer = new System.Timers.Timer(5000);Set elapsed event for the timer. This occurs when the interval elapses −timer.Elapsed += OnTimedEvent;Now start the timer.timer.Enabled = true;Exampleusing System; using System.Timers; public class Demo {    private static Timer timer;    public static void Main() {       timer = new System.Timers.Timer();       timer.Interval = 5000;       timer.Elapsed += OnTimedEvent;     ...

Read More

Swap two Strings without using temp variable in C#

Samual Sam
Samual Sam
Updated on 21-Jun-2020 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
Showing 1471–1480 of 1,951 articles
« Prev 1 146 147 148 149 150 196 Next »
Advertisements