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
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 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
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
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(); } } }
As an alternative of Packages in Java, the C# language has namespace.Packages in JavaPackages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.Namespace in C#A namespace is designed for providing a way to keep one set of names separate from another. The class names declared in one namespace does not conflict with the same class names declared in another.A namespace definition begins with the keyword namespace followed by the namespace name. The following shows how to work with namespace in C# −Exampleusing System; ... Read More
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 C#.Regex rgx = new Regex(pattern); string result = rgx.Replace(input, replacement);The following is the complete example −Exampleusing System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string input = " Welcome User "; ... Read More
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP