Display IP Address of the Machine Using C#

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

827 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();    } }

Display the Name of the Current Thread in C#

Arjun Thakur
Updated on 20-Jun-2020 15:08:48

2K+ Views

Use the Name property to display the name of the current thread in C#.Firstly, use the currentThread property to display information about a thread −Thread thread = Thread.CurrentThread;Now use the thread.Name property to display name of the thread −thread.NameLet us see the complete code show current thread’s name in C# −Example Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My Thread";          Console.WriteLine("Thread Name = {0}", thread.Name);          Console.ReadKey();       }    } }OutputThread Name = My Thread

Use hasherror and hashwarning Directives in C#

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

579 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

Use hashif, hashelif, hashelse, and hashendif Directives in C#

Ankith Reddy
Updated on 20-Jun-2020 15:07:13

1K+ Views

All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not statements, so they do not end with a semicolon (;).#ifThe #if directive allows testing a symbol or symbols to see if they evaluate to true.#elseIt allows to create a compound conditional directive, along with #if.#elifIt allows creating a compound conditional directive.#endifThe #endif specifies the end of a conditional directive.The following is an example showing the usage of #if, #elif, #else and #endif directives −Example Live Demo#define One #undef Two using System; namespace Demo {    class ... Read More

Push vs Pop in Stack Class in C#

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

686 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

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

Iterator Functions in C#

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

869 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

Declare a Delegate in C#

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

324 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

Declare a Tuple in C#

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

516 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

Define the Rank of an Array in C#

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

467 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

Advertisements