Category: C# Questions

How to Get HostName in C#/CSharp

To get host name of the local computer, we make use of GetHostName() method which resides in Dns class.  Dns class is part of System.Net space.  Example is given below. using System; using System.Net; namespace Hello_World { class Program { static void Main(string args) { string hostname = Dns.GetHostName(); Console.WriteLine(hostname); } } }

How to Trim a String in C#/CSharp

To trim a string in C#/CSharp, we make use of Trim() method.  This will remove any extra space before and after the string.  Example is given. using System; namespace Hello_World { class Program { static void Main(string args) { string hello = " Hello World "; Console.WriteLine("Before: " + hello ); Console.WriteLine("After: " + hello.Trim());

How to Join Two Strings in C#/CSharp

To join strings in C#/CSharp, we make use of Concat method.  Example is given below. using System; namespace Hello_World { class Program { static void Main(string args) { string hello = "Hello "; string world = " World!"; string final = string.Concat(hello, world); Console.WriteLine(final); } } }