Found 2628 Articles for Csharp

How to demonstrate Prefix Operator using C#?

Ankith Reddy
Updated on 20-Jun-2020 15:16:55

112 Views

The increment operator is ++ operator. If used as prefix on a variable, the value of variable gets incremented by 1. After that the value is returned unlike Postfix operator. It is called Prefix increment operator. In the same way the decrement operator works but it decrements by 1.For example, ++a;The following is an example demonstrating Prefix increment operator −Example Live Demousing System; class Program {    static void Main() {       int a, b;       a = 10;       Console.WriteLine(++a);       b = a;       Console.WriteLine(a);       Console.WriteLine(b);    } }Output11 11 11

How to capture null reference exception in C#?

Arjun Thakur
Updated on 20-Jun-2020 14:54:23

227 Views

It handles errors generated from referencing a null object. The Null reference exception occurs when you are looking to access member fields or function types that points to null.Let’s say we have the following null string −string str = null;Now you try to get the length of the null string, then it would cause an exception −If(str.Length == null) {}Above the exception will be thrown. Now let us seen how to prevent the null pointer exception to be thrown −Example Live Demousing System; class Program {    static void Main() {       int[] arr = new int[5] {1, ... Read More

How to use #undef directive in C#?

karthikeya Boyini
Updated on 20-Jun-2020 14:55:18

205 Views

The #undef directive allows you to undefine a symbol. The following is the syntax −#undef SYMBOLFor example,#undef OneIt evaluates to false when used along with #if directive. Let us see an example −Example Live Demo#define One #undef Two using System; namespace Demo {    class Program {       static void Main(string[] args) {          #if (One && TWO)          Console.WriteLine("Both are defined");          #elif (ONE && !TWO)          Console.WriteLine("ONE is defined and TWO is undefined");          #elif (!ONE && TWO)          Console.WriteLine("ONE is defined and TWO is undefined");          #else          Console.WriteLine("Both are undefined");          #endif       }    } }OutputBoth are undefined

How to define the rank of an array in C#?

Samual Sam
Updated on 20-Jun-2020 14:55:53

309 Views

To find the number of dimensions of an array, use the Array Rank property. This is how you can define it −arr.RankHere, arr is our array −int[,] arr = new int[3,4];If you want to get the rows and columns it has, then uses the GetLength property −arr.GetLength(0); arr.GetLength(1);The following is the complete code −Example Live Demousing System; class Program {    static void Main() {       int[,] arr = new int[3,4];       Console.WriteLine(arr.GetLength(0));       Console.WriteLine(arr.GetLength(1));       // Length       Console.WriteLine(arr.Length);       Console.WriteLine("Dimensions of Array : " + arr.Rank);    } }Output3 4 12 Dimensions of Array : 2

Jagged Array in C#

Samual Sam
Updated on 20-Jun-2020 14:58:22

255 Views

A Jagged array is an array of arrays. You can declare a jagged array named points of type int as −int [][] points;Let us now see how to initialize it −int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, 80}, new int[]{ 60, 70 }};Access the jagged array element as −points[i][j]);The following is the complete example showing how to work with jagged arrays in C# −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          int[][] pages = new int[][]{new int[]{400, 345}, new ... Read More

How to declare a tuple in C#?

Chandu yadav
Updated on 20-Jun-2020 14:57:34

389 Views

To declare a tuple the following is the format wherein we have a tuple with int and string items −Tuple tuple = new Tuple(20, "Tom");Now, check for first item in the tuple, which is an integer −if (tuple.Item1 == 99) {    Console.WriteLine(tuple.Item1); }Now check for second item in the tuple, which is a string − if (tuple.Item2 == "Tim") {    Console.WriteLine(tuple.Item2); }The following is an example to create a tuple with string and int items −using System; using System.Threading; namespace Demo {    class Program {       static void Main(string[] args) {   ... Read More

How to declare a delegate in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:00:01

198 Views

A delegate in C# is a reference to the method. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.Let us see how to declare delegates in C# −delegate Let us see an example to learn how to work with Delegates in C# −Example Live Demousing System; using System.IO; namespace DelegateAppl {    class PrintString {       static FileStream fs;       static StreamWriter ... Read More

Iterator Functions in C#

Samual Sam
Updated on 20-Jun-2020 15:01:31

601 Views

An iterator method performs a custom iteration over a collection. It uses the yield return statement and returns each element one at a time. The iterator remembers the current location and in the next iteration the next element is returned.The following is an example −Example Live Demousing System; using System.Collections.Generic; using System.Linq; namespace Demo {    class Program {       public static IEnumerable display() {          int[] arr = new int[] {99, 45, 76};          foreach (var val in arr) {             yield return val.ToString();   ... Read More

Push vs pop in stack class in C#

karthikeya Boyini
Updated on 20-Jun-2020 15:04:44

384 Views

Stack class represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items.The following is the property of Stack class −Count − Gets the number of elements in the stack.Push OperationAdd elements in the stack using the Push operation −Stack st = new Stack(); st.Push('A'); st.Push('B'); st.Push('C'); st.Push('D');Pop OperationThe Pop operation removes elements from the stack starting from the element on the top.Here is an example showing how to work with Stack class and its Push() and Pop() method −Using System; using System.Collections; namespace CollectionsApplication {    class Program ... Read More

How to write single-line comments in C#?

Samual Sam
Updated on 20-Jun-2020 14:42:22

171 Views

If you want to add a comment that restricts itself to a single line, then use the single-line comments −// variable int i = 20;The following is a sample C# program showing how to add single-line comments −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          // display text          Console.WriteLine("Hello World");          Console.ReadKey();       }    } }OutputHello World

Advertisements