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
Articles by karthikeya Boyini
Page 20 of 143
C# program to count upper and lower case characters in a given string
To count uppercase and lowercase characters in a string, we can iterate through each character and check if it falls within specific ASCII ranges. For uppercase letters, we check if the character is between 'A' and 'Z', while for lowercase letters, we check if it's between 'a' and 'z'. Syntax Following is the syntax for checking uppercase characters − if (myStr[i] >= 'A' && myStr[i] = 'a' && myStr[i] = 'a' && myStr[i] = 'A' && myStr[i]
Read MoreC# program to Display Hostname and IP address
To find the hostname of the current machine, use the Dns.GetHostName() method in C#. This method returns the host name of the local computer as a string. To get the IP addresses, use the IPHostEntry.AddressList property which provides an array of all IP addresses associated with the hostname. Syntax Following is the syntax for getting the hostname − string hostName = Dns.GetHostName(); Following is the syntax for getting IP addresses − IPHostEntry hostEntry = Dns.GetHostEntry(hostName); IPAddress[] addresses = hostEntry.AddressList; Example The following code demonstrates how to display hostname and ...
Read MoreC# program to replace all spaces in a string with '%20'
When working with strings in C#, you may need to replace spaces with '%20' for URL encoding or other formatting purposes. The Replace() method provides a simple way to substitute all occurrences of a character or substring with another value. Syntax Following is the syntax for using the Replace() method − string newString = originalString.Replace(oldValue, newValue); Parameters oldValue − The string or character to be replaced newValue − The string or character to replace with Return Value The method returns a new string with all occurrences of the specified ...
Read MoreWhat is the difference between a class and an object in C#?
When you define a class, you define a blueprint or template for a data type. The object is an instance of that class − a concrete implementation created from the blueprint. A class defines the structure and behavior, while objects are the actual entities that hold data and perform actions. The methods and variables that constitute a class are called members of the class. To access class members, you use the dot (.) operator after the object name. The dot operator links the name of an object with the name of a member. Class vs Object ...
Read MoreC# Linq ElementAt Method
The ElementAt() method in C# LINQ returns the element at a specified index position in a sequence. It is part of the LINQ extension methods and can be used with any IEnumerable collection. The ElementAt() method throws an ArgumentOutOfRangeException if the index is out of bounds. For safer access, you can use ElementAtOrDefault(), which returns the default value for the type if the index is invalid. Syntax Following is the syntax for the ElementAt() method − public static TSource ElementAt(this IEnumerable source, int index) Following is the syntax for the ElementAtOrDefault() method − ...
Read MoreC# program to find IP Address of the client
To find the IP address of the client machine in C#, we use the DNS (Domain Name System) class from the System.Net namespace. This involves getting the hostname first and then resolving it to obtain the associated IP addresses. Syntax Following is the syntax to get the hostname − string hostName = Dns.GetHostName(); Following is the syntax to get IP addresses from hostname − IPHostEntry hostEntry = Dns.GetHostEntry(hostName); IPAddress[] addresses = hostEntry.AddressList; How It Works The process involves two main steps: Dns.GetHostName() retrieves the hostname of the ...
Read MoreC# Program to Illustrate Lower Triangular Matrix
A lower triangular matrix is a square matrix where all elements above the main diagonal are zero. In other words, for any element at position (i, j), if j > i, then the element is zero. The main diagonal consists of elements where the row index equals the column index (i = j). Below the diagonal, elements can have any non-zero values. Syntax The condition to check if an element should be displayed or set to zero − if (i >= j) // Display the actual element else ...
Read MoreC# Math.DivRem Method
The Math.DivRem method in C# performs integer division and returns both the quotient and remainder in a single operation. This is more efficient than performing separate division and modulo operations when you need both results. Syntax Following is the syntax for the Math.DivRem method − public static int Math.DivRem(int a, int b, out int result); public static long Math.DivRem(long a, long b, out long result); Parameters a − The dividend (number to be divided) b − The divisor (number to divide by) result − An out parameter that receives the remainder ...
Read MoreC# Program to Kill a Thread
In C#, a thread can be gracefully stopped using a flag-based approach rather than forcibly terminating it. This method allows the thread to complete its current work and exit cleanly by checking a boolean flag in its loop condition. The recommended approach involves setting a boolean flag that the thread periodically checks. When you want to stop the thread, you set the flag to true, causing the thread's loop to exit naturally. Syntax Following is the syntax for creating and controlling a thread with a stop flag − Thread thread = new Thread(methodName); thread.Start(); ...
Read MoreMathematical Functions in C#
The System.Math class in C# provides methods and properties to perform mathematical operations, trigonometric calculations, logarithmic functions, and other common mathematical computations. All methods in the Math class are static, meaning you can call them directly without creating an instance. Common Mathematical Methods The following table shows some of the most commonly used methods in the Math class − Method Description Abs() Returns the absolute value of a number (works with decimal, double, int, etc.) Ceiling() Returns the smallest integer greater than or equal to the specified number ...
Read More