Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Csharp Articles
Page 164 of 196
C# program to find IP Address of the client
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 MoreC# Program to display priority of Thread
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 MoreC# program to display factors of entered number
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 MoreC# program to convert decimal to Octal number
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 MoreC# Program to Convert Fahrenheit to Celsius
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 MoreC# program to check password validity
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 MoreC# Generics vs C++ Templates
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 MoreC# program to check for URL in a String
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 MoreC# program to check if there are K consecutive 1's in a binary number
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 MoreC# and Multiple Inheritance
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