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
Csharp Articles
Page 66 of 196
What are the differences between public, protected and private access specifiers in C#?
Public Access SpecifierPublic access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.Exampleusing System; namespace Demo { class Rectangle { public double length; public double width; public double GetArea() { return length * width; } public void Display() { Console.WriteLine("Length: {0}", length); Console.WriteLine("Width: {0}", width); Console.WriteLine("Area: {0}", GetArea()); ...
Read MoreWhat are Add, Remove methods in C# lists?
The List is a collection in C# and is a generic collection. The add and remove methods are used in C# lists for adding and removing elements.Let us see how to use Add() method in C#.Exampleusing System; using System.Collections.Generic; class Program { static void Main() { List sports = new List(); sports.Add("Football"); sports.Add("Tennis"); sports.Add("Soccer"); foreach (string s in sports) { Console.WriteLine(s); } } }OutputFootball Tennis SoccerLet us see how to use Remove() method in C#.Exampleusing System; ...
Read MoreWhat are all the possible C# array initialization syntaxes?\\n
Array can be initialized in more than one ways in C#. Let us see some example.Method OneUsing size of array.int [] marks = new int[5] { 99, 98, 92, 97, 95};Method TwoBy omitting the size.int [] marks = new int[] { 99, 98, 92, 97, 95};Method ThreeInitializing at the time of declaration.int [] marks = { 99, 98, 92, 97, 95};Let us see one of the ways to initialize arrays in C#.Exampleusing System; namespace Demo { class MyArray { static void Main(string[] args) { int [] n = new int[10]; /* n ...
Read MoreWhat are Base and Derived Classes in C#?
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.For example, Vehicle Base class with the following Derived Classes.Truck Bus MotobikeThe derived class inherits the base class member variables and member methods.In the same way, the derived class for Shape class can be Rectangle as in the following example.Exampleusing System; namespace Program { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) ...
Read MoreHow to use StringBuilder in C#?
With StringBuilder, you can expand the number of characters in the string. String cannot be changed once it is created, but StringBuildercan be expanded. It does not create a new object in the memory.Initialize StringBuilder.StringBuilder str = new StringBuilder();Let us see an example to learn how to work with StringBuilder in C#.Exampleusing System; using System.Text; public class Program { public static void Main() { StringBuilder str = new StringBuilder("Web World!!",30); str.Replace("World", "Arena"); Console.WriteLine(str); } }OutputWeb Arena!!Above the Replace() method of StringBuilder is used to to replace a string in C#.
Read MoreHow to use the ?: conditional operator in C#?
A conditional operator is represented by the symbol '?:' The first operand is the evaluating expression. It has right to left associativity.The syntax for conditional operator.expression ? expression : expressionThe conditional operator works as follows −The first operand is implicitly converted to bool.If the first operand evaluates to true, the second operand is evaluated.If the first operand evaluates to false, the third operand is evaluated.Remember, only one of the last two operands is evaluated in a conditional expression.Exampleusing System; namespace Demo { class Program { static void Main(string[] args) { int num1 ...
Read MoreHow to use the Copy(, ,) method of array class in C#
As the name suggests the Array.Copy() method in C# is used to copy elements of one array to another array.The following is the syntax.Array.Copy(src, dest, length);Heresrc = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C#.Exampleusing System; class Program { static void Main() { int[] arrSource = new int[4]; arrSource[0] = 99; arrSource[1] = 66; arrSource[2] = 111; arrSource[3] = 33; int[] arrTarget = new int[4]; Array.Copy(arrSource, arrTarget, 4); Console.WriteLine("Destination Array ..."); foreach (int value in arrTarget) { Console.WriteLine(value); } } }OutputDestination Array ... 99 66 111 33
Read MoreHow to use the GetLowerBound method of array class in C#
The GetLowerBound() method of array class in C# gets the lower bound of the specified dimension in the Array.Firstly, set the array and get the lower bound as shown below −arr.GetLowerBound(0).ToString()The following is an example stating the usage of GetLowerBound() method in C#.Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower { class Program { static void Main(string[] args) { Array arr = Array.CreateInstance(typeof(String), 3); arr.SetValue("Car", 0); arr.SetValue("Truck", 1); arr.SetValue("Motorbike", 2); Console.WriteLine("Lower Bound {0}",arr.GetLowerBound(0).ToString()); Console.ReadLine(); } } }OutputLower Bound 0
Read MoreHow to sort a list in C#?
Set a list with some values. Here, we have a list of strings.var cars = new List() {"Mercedes", "Audi", "Jaguar" };To sort, simply use Sort() method.cars.Sort();The following is an example showing how to sort a list in C#.Exampleusing System; using System.Collections.Generic; public class Program { public static void Main() { var cars = new List() {"Mercedes", "Audi", "Jaguar" }; Console.WriteLine("Original Array ="); foreach (var name in cars) { Console.WriteLine(name); } // sort cars.Sort(); Console.WriteLine("Sorted Array ="); foreach (var name in cars) { Console.WriteLine(name); } } }OutputOriginal Array = Mercedes Audi Jaguar Sorted Array = Audi Jaguar Mercedes
Read MoreHow to use the GetValue() method of array class in C#?
The GetValue() method of array class in C# gets the value at the specified position in the one-dimensional Array. The index is specified as a 32-bit integer.We have set the array values first using the Array.CreateInstance method.Array arr = Array.CreateInstance(typeof(String), 3, 6); arr.SetValue("One", 0, 0); arr.SetValue("Two", 0, 1); arr.SetValue("Three", 0, 2); arr.SetValue("Four", 0, 3); arr.SetValue("Five", 1, 4); arr.SetValue("Six", 1, 5); arr.SetValue("Seven", 1, 2); arr.SetValue("Eight", 1, 3);Then loop throught the array length. This will display all the values using the GetValue() method.for (int i = 0; i < a; i++) for (int j = 0; j < b; j++) Console.WriteLine( arr.GetValue(i, ...
Read More