Programming Articles - Page 3156 of 3363

How to use #error and #warning directives in C#?

Samual Sam
Updated on 20-Jun-2020 15:08:10

577 Views

#error directiveThe #error directive allows generating an error from a specific location in your code.Let us see an example −Example Live Demousing System; namespace Demo {    class Program {       public static void Main(string[] args) {          #if (!ONE)          #error ONE is undefined          #endif          Console.WriteLine("Generating a user-defined error!");       }    } }After running the above program, a user-defined error generates −OutputCompilation failed: 1 error(s), 0 warnings error CS1029: #error: 'ONE is undefined'#warning directiveThe #warning directive allows generating a level ... Read More

How to display the IP Address of the Machine using C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:10:52

825 Views

Use the IPHostEntry.AddressList Property to get IP Address −IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;Try the following code to display IP address −Exampleusing System; using System.Net; class Program {    static void Main() {       String hostName = string.Empty;       hostName = Dns.GetHostName();       Console.WriteLine("Hostname: "+hostName);       IPHostEntry myIP = Dns.GetHostEntry(hostName);       IPAddress[] address = myIP.AddressList;       for (int i = 0; i < address.Length; i++) {          Console.WriteLine("IP Address {1} : ",address[i].ToString());       }       Console.ReadLine();    } }

How to destroy threads in C#?

Chandu yadav
Updated on 20-Jun-2020 15:12:48

865 Views

The Abort() method is used for destroying threads.The runtime aborts the thread by throwing a ThreadAbortException. This exception cannot be caught, the control is sent to the finally block, if any.The following is an example showing how to destroy threads −Example Live Demousing System; using System.Threading; namespace MultithreadingApplication {    class ThreadCreationProgram {       public static void CallToChildThread() {          try {             Console.WriteLine("Child thread starts");             // do some work, like counting to 10             for (int counter = 0; counter

What does the keyword var do in C#?

Samual Sam
Updated on 20-Jun-2020 15:13:58

473 Views

The "var" keyword initializes variables with var support. Just assign whatever value you want for the variable, integer, string, float, etc.Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          var myInt = 5;          var myString = "Amit";          Console.WriteLine("Rank: {0} Name: {1}",myInt,myString);       }    } }OutputRank: 5 Name: AmitWe can also use var in arrays −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          var myInt = new int[] {65,43,88,56};          foreach(var val in myInt)          Console.WriteLine(val);       }    } }Output65 43 88 56

How to declare a two-dimensional array in C#

George John
Updated on 20-Jun-2020 15:14:51

845 Views

A 2-dimensional array is a list of one-dimensional arrays. Declare it like the two dimensional array shown below −int [, ] aTwo-dimensional arrays may be initialized by specifying bracketed values for each row.int [, ] a = new int [4, 4] { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} , {12, 13, 14, 15} };The following is an example showing how to work with two-dimensional arrays in C# −Example Live Demousing System; namespace ArrayApplication {    class MyArray {       static void Main(string[] args) {          /* an ... Read More

How to demonstrate Prefix Operator using C#?

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

282 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

355 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

305 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

464 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

512 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

Advertisements