Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by karthikeya Boyini
Page 47 of 143
'this' keyword in C#
The “this” keyword in C# is used to refer to the current instance of the class. It is also used to differentiate between the method parameters and class fields if they both have the same name.Another usage of “this” keyword is to call another constructor from a constructor in the same class.Here, for an example, we are showing a record of Students i.e: id, Name, Age, and Subject. To refer to the fields of the current class, we have used the “this” keyword in C# −public Student(int id, String name, int age, String subject) { this.id = id; ...
Read MoreDate format validation using C# Regex
Use the DateTime.TryParseExact method in C# for Date Format validation.They method converts the specified string representation of a date and time to its DateTime equivalent. It checks whether the entered date format is correct or not.Exampleusing System; using System.Globalization; namespace Demo { class Program { static void Main(string[] args) { DateTime d; bool chValidity = DateTime.TryParseExact( "08/14/2018", "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d); Console.WriteLine(chValidity); } } }OutputTrue
Read MoreC# Program to convert integer array to string array
Use the ConvertAll method to convert integer array to string array.Set an integer array −int[] intArray = new int[5]; // Integer array with 5 elements intArray[0] = 15; intArray[1] = 30; intArray[2] = 44; intArray[3] = 50; intArray[4] = 66;Now use Array.ConvertAll() method to convert integer array to string array −Array.ConvertAll(intArray, ele => ele.ToString());Let us see the complete code −Exampleusing System; using System.Text; public class Demo { public static void Main() { int[] intArray = new int[5]; // Integer array with 5 elements intArray[0] = 15; intArray[1] = 30; intArray[2] = 44; intArray[3] = 50; intArray[4] = 66; string[] strArray = Array.ConvertAll(intArray, ele => ele.ToString()); Console.WriteLine(string.Join("|", strArray)); } }Output15|30|44|50|66
Read MoreAbstract Classes in C#
An abstract class in C# includes abstract and non-abstract methods. A class is declared abstract to be an abstract class. You cannot instantiate an abstract class.Let us see an example, wherein we have an abstract class Vehicle and abstract method display()−public abstract class Vehicle { public abstract void display(); } The abstract class has derived classes: Bus, Car, and Motorcycle. The following is an implementation of the Bus derived class −public class Bus : Vehicle { public override void display() { Console.WriteLine("Bus"); } } ExampleLet us see the complete example of abstract classes in C# −using ...
Read MoreC# program to merge two sorted arrays into one
Set two arrays that you wish to merge −int[] arr1 = new int[5] { 5, 15, 25, 30, 47 }; int[] arr2 = new int[5] { 55, 60, 76, 83, 95 };Now take a third array that would merge both the above arrays −int[] merged = new int[10];The following is the code that merges two arrays into the third array in C# −Exampleusing System; using System.Collections.Generic; class Program { static void Main() { int i = 0; int j = 0; ...
Read MoreShow Bootstrap class
To show a Bootstrap class, use the .show class. You can try to run the following code to make a class visibleExample Bootstrap Example This class is visible. This is an example for hide class. This will be hidden.
Read MoreClear a list in C#
Firstly, set a list −List myList = new List(); myList.Add(45); myList.Add(77);Now, to clear the above list, use Clear() −myList.Clear();Here is the complete code −Exampleusing System; using System.Collections.Generic; public class Demo { public static void Main() { List myList = new List(); myList.Add(45); myList.Add(77); Console.WriteLine("Elements: "+myList.Count); myList.Clear(); Console.WriteLine("Elements after using clear: "+myList.Count); } }OutputElements: 2 Elements after using clear: 0
Read MoreHow to write the first program in C#?
The following is the first program in C# programming −Exampleusing System; namespace MyHelloWorldApplication { class MyDemoClass { static void Main(string[] args) { // display text Console.WriteLine("Hello World"); // display another text Console.WriteLine("Welcome!"); Console.ReadKey(); } } }OutputHello World Welcome!Let us see now what all it includes.using System; - the using keyword is used to include the System namespace in the program.namespace declaration - A namespace is a collection ...
Read MoreFlow control in try catch finally in C#
The flow control in try, catch, and finally can be understood using the following example. Here, we are dividing two numbers −Exampleusing System; namespace ErrorHandlingApplication { class DivNumbers { int result; DivNumbers() { result = 0; } public void division(int num1, int num2) { try { result = num1 / num2; } catch (DivideByZeroException e) { Console.WriteLine("Exception caught: {0}", e); ...
Read MoreC# program to find the index of an element in a List
Set a list and add elements −List val = new List(); // integer elements val.Add(35); val.Add(55); val.Add(68);Let’s say now we need to find the index of element 68. For that, use the IndexOf() method −int index = val.IndexOf(68);Here is the complete code −Exampleusing System; using System.Collections.Generic; public class Demo { public static void Main() { List val = new List(); // integer elements val.Add(35); val.Add(55); val.Add(68); // finding the index of element 68 int index = val.IndexOf(68); Console.WriteLine(index); } }Output2
Read More