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 40 of 143
How to check if a string is a valid keyword in C#?
To check if a string is a valid keyword, use the IsValidIdentifier method.The IsValidIdentifier method checks whether the entered value is an identifier or not. If it’s not an identifier, then it’s a keyword in C#.Let us see an example, wherein we have set the CodeDomProvider and worked with the IsValiddentifier method −CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");Let us see the complete codeLExampleusing System; using System.IO; using System.CodeDom.Compiler; namespace Program { class Demo { static void Main(string[] args) { string str1 = "amit"; string str2 = "for"; ...
Read MoreAdd buttons in Bootstrap Navbar
Add buttons using class .navbar-btn to elements. You can try to run the following code to add a button to the navbarExample Bootstrap Example Example Submit Button Navbar Button
Read MoreHow do you loop through a C# array?
To loop through an array in C#, use any of the loops. These loops have starting and ending value set that allows you to set or check value through iterations.C# has while, do…while, for and foreach loops to loop through an array.Let us see an example of for loop in C# −Exampleusing System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int [] n = new int[10]; int i, j; for ( i = 0; i < 10; i++ ...
Read MoreSingleton Class in C#
Singleton Class allow for single allocations and instances of data. It has normal methods and you can call it using an instance.To prevent multiple instances of the class, the private constructor is used.Let us see an example −public class Singleton { static Singleton b = null; private Singleton() { } }The following is another example displaying how to display Singleton class −Exampleusing System; class Singleton { public static readonly Singleton _obj = new Singleton(); public void Display() { Console.WriteLine(true); } Singleton() {} } class Demo { public static void Main() { Singleton._obj.Display(); } }OutputTrue
Read MoreWhat are destructors in C# programs?
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope.It has exactly the same name as that of the class with a prefixed tilde (~), for example, our class name is Demo.public Demo() { // constructor Console.WriteLine("Object is being created"); } ~Demo() { //destructor Console.WriteLine("Object is being deleted"); }Let us see an example to learn how to work with Destructor in C#.Exampleusing System; namespace LineApplication { class Line { private double length; // Length of a line ...
Read MoreC# program to copy a range of bytes from one array to another
Use the Buffer.BlockCopy method to copy a range of bytes from one array to another −Set a byte array −byte[] b1 = new byte[] {22, 49}; byte[] b2 = new byte[5];Copy bytes from one array to another −Buffer.BlockCopy(b1, 0, b2, 0, 2);The following is the complete code −Exampleusing System; class Demo { static void Main(){ // byte arrays byte[] b1 = new byte[] {22, 49}; byte[] b2 = new byte[5]; // copying bytes from one to another Buffer.BlockCopy(b1, 0, b2, 0, 2); ...
Read More‘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 More