Csharp Articles

Page 164 of 196

C# program to find IP Address of the client

karthikeya Boyini
karthikeya Boyini
Updated on 19-Jun-2020 2K+ Views

Firstly find the hostname using the Dns.GetHostName() method in C# −String hostName = string.Empty; hostName = Dns.GetHostName(); Console.WriteLine("Hostname: "+hostName);Now, use the IPHostEntry.AddressList Property to get IP Address −IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;ExampleTry the following code to display IP address −using System; using System.Net; class Program {    static void Main() {       String hostName = string.Empty;       hostName = Dns.GetHostName();       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();    } }

Read More

C# Program to display priority of Thread

Samual Sam
Samual Sam
Updated on 19-Jun-2020 447 Views

To show the priority of the thread in C#, use the Priority property. Firstly, use the currentThread property to display information about a thread − Thread thread = Thread.CurrentThread; Now use the thread.Priority property to display the priority of the thread − thread.Priority Example Let us see the complete code to show the thread's priority in C#. using System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My Thread";          Console.WriteLine("Thread Priority = {0}", thread.Priority);          Console.ReadKey();       }    } } Output Thread Priority = Normal

Read More

C# program to display factors of entered number

karthikeya Boyini
karthikeya Boyini
Updated on 19-Jun-2020 1K+ Views

Firstly, enter the number for which you want the factors −Console.WriteLine("Enter the Number:"); n = int.Parse(Console.ReadLine());After that, loop through to find the factors −for (i = 1; i

Read More

C# program to convert decimal to Octal number

Samual Sam
Samual Sam
Updated on 19-Jun-2020 817 Views

Set the decimal number – int decVal = 40; Now take a variable and set the decVal in it. Find the remainder with 8 since octal has base-8 number system and evaluate it in a loop like the following code snippet. while (quot != 0) { octalVal[i++] = quot % 8; quot = quot / 8; } Example You can try to run the following code to convert decimal to octal number. using System; class Demo {    public static void Main() {       int decVal, quot, i = 1, j;       int[] octalVal = new int[80]; ...

Read More

C# Program to Convert Fahrenheit to Celsius

Samual Sam
Samual Sam
Updated on 19-Jun-2020 3K+ Views

Firstly, set the Fahrenheit temperature − double fahrenheit = 97; Console.WriteLine("Fahrenheit: " + fahrenheit); Now convert it into Celsius − celsius = (fahrenheit - 32) * 5 / 9; Example You can try to run the following code to convert Fahrenheit to Celsius. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          double celsius;          double fahrenheit = 97;          Console.WriteLine("Fahrenheit: " + fahrenheit);          celsius = (fahrenheit - 32) * 5 / 9;          Console.WriteLine("Celsius: " + celsius);          Console.ReadLine();       }    } } Output Fahrenheit: 97 Celsius: 36.1111111111111

Read More

C# program to check password validity

Samual Sam
Samual Sam
Updated on 19-Jun-2020 942 Views

While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −Min 8 char and max 14 charOne lower caseNo white spaceOne upper caseOne special charLet us see how to check the conditions one by one −Min 8 char and max 14 charif (passwd.Length < 8 || passwd.Length > 14) return false;Atleast one lower caseif (!passwd.Any(char.IsLower)) return false;No white spaceif (passwd.Contains(" ")) return false;One upper caseif (!passwd.Any(char.IsUpper)) return false;Check for one special characterstring specialCh = @"%!@#$%^&*()?/>.

Read More

C# Generics vs C++ Templates

karthikeya Boyini
karthikeya Boyini
Updated on 19-Jun-2020 697 Views

C# Generics and C++ Templates provide support for parameterized types. The following are the differences −FlexibilityC++ Templates are more flexible than C# GenericsExplicit specializationExplicit specialization is not supported by C#Type ParameterThe type parameter cannot be used as the base class for the generic type in C#C# does not allow type parameters to have default types.Run-TimeThe C++ template has a compile-time modal, whereas C# Generics is both compile and run-time. Generics have run-time support.Non-type template parametersC#Templates will not allow non-type template parameters.Partial SpecializationC# does not even support partial specialization.

Read More

C# program to check for URL in a String

Samual Sam
Samual Sam
Updated on 19-Jun-2020 3K+ Views

Use the StartWith() method in C# to check for URL in a String. Let us say our input string is − string input = "https://example.com/new.html"; Now we need to check for www or non-www link. For this, use the if statement in C# − if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) { } Example You can try to run the following code to check for URL in a string. using System; class Demo {     static void Main() {         string input = "https://example.com/new.html";         // See if input matches one of these starts.         if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {             Console.WriteLine(true);         }     } } Output True

Read More

C# program to check if there are K consecutive 1's in a binary number

Samual Sam
Samual Sam
Updated on 19-Jun-2020 332 Views

To check for consecutive 1's in a binary number, you need to check for 0 and 1. Firstly, set a bool array for 0s and 1s i.e. false and true − bool []myArr = {false, true, false, false, false, true, true, true}; For 0, set the count to 0 − if (myArr[i] == false) count = 0; For 1, increment the count and set the result. The Max() method returns the larger of two number − count++; res = Math.Max(res, count); Example The following is the example to check if there are K consecutive 1's in a ...

Read More

C# and Multiple Inheritance

Samual Sam
Samual Sam
Updated on 19-Jun-2020 6K+ Views

Multiple Inheritance isn’t supported in C#. To implement multiple inheritances, use Interfaces.Here is our interface PaintCost in class Shape −public interface PaintCost { int getCost(int area); }The shape is our base class whereas Rectangle is the derived class −class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 80; } }Let us now see the complete code to implement Interfaces ...

Read More
Showing 1631–1640 of 1,951 articles
« Prev 1 162 163 164 165 166 196 Next »
Advertisements