Found 2587 Articles for Csharp

How to use #undef directive in C#?

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

278 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

424 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

How to declare a tuple in C#?

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

484 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

289 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

839 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

621 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

321 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

Stack and Queue in C#

Ankith Reddy
Updated on 20-Jun-2020 15:02:54

6K+ Views

StackStack 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.The following are the methods of Stack class −Sr.No.Method & Description1public virtual void Clear();Removes all elements from the Stack.2public virtual bool Contains(object obj);Determines whether an element is in the Stack.3public virtual object Peek();Returns the object at the top of the Stack without removing it.4public virtual object Pop();Removes and returns the object at the top of the Stack.5public virtual void Push(object obj);Inserts an object ... Read More

Ternary Operator in C#

karthikeya Boyini
Updated on 20-Jun-2020 14:43:03

3K+ Views

Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.For example −b = (a == 1) ? 20 : 30;Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.The following is an example −Example Live Demousing System; namespace DEMO {    class Program {       static void Main(string[] args) {          int a, b;          a = 10;          b = (a == 1) ? 20 : 30;          Console.WriteLine("Value of b is {0}", b);          b = (a == 10) ? 20 : 30;          Console.WriteLine("Value of b is {0}", b);          Console.ReadLine();       }    } }OutputValue of b is 30 Value of b is 20

static keyword in C#

George John
Updated on 20-Jun-2020 14:44:40

3K+ Views

We can define class members as static using the static keyword. When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member.The keyword static implies that only one instance of the member exists for a class. Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it.The following is an example showing the usage of static variables −Example Live Demousing System; namespace StaticVarApplication {    class StaticVar { ... Read More

Advertisements