Found 2745 Articles for Csharp

C# program to split and join a string

Samual Sam
Updated on 19-Jun-2020 11:39:37

2K+ Views

To split and join a string in C#, use the split() and join() method. Let us say the following is our string −string str = "This is our Demo String";To split the string, we will use the split() method −var arr = str.Split(' ');Now to join, use the join() method and join rest of the string. Here, we have skipped the part of the string using the skip() method −string rest = string.Join(" ", arr.Skip(1));ExampleYou can try to run the following code in C# to split and join a string.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ... Read More

C# Program to perform Celsius to Fahrenheit Conversion

karthikeya Boyini
Updated on 19-Jun-2020 11:41:12

6K+ Views

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

C# Program to perform all Basic Arithmetic Operations

Samual Sam
Updated on 19-Jun-2020 11:44:15

6K+ Views

Basic Arithmetic Operators in C#, include the following −OperatorDescription+Adds two operands-Subtracts the second operand from the first*Multiplies both operands/Divides the numerator by de-numerator%Modulus Operator and remainder of after an integer division++Increment operator increases integer value by one--Decrement operator decreases integer value by oneTo add, use the Addition Operator −num1 + num2;In the same way, it works for Subtraction, Multiplication, Division, and other operators.ExampleLet us see a complete example to learn how to implement Arithmetic operators in C#.Live Demousing System; namespace Sample {    class Demo {       static void Main(string[] args) {          int num1 ... Read More

C# Program to Pause a Thread

karthikeya Boyini
Updated on 19-Jun-2020 11:46:34

6K+ Views

To pause a thread in C#, use the sleep() method.You need to set the milliseconds for which you want the thread to pause, for example, for 5 seconds, use −Thread.Sleep(5000);ExampleLet us see how to loop through and set the sleep method to pause the thread.Live Demousing System; using System.Threading; namespace Sample {    class Demo {       static void Main(string[] args) {          for (int i = 0; i < 10; i++) {             Console.WriteLine("Sleep for 1 second!");             Thread.Sleep(1000);          } ... Read More

C# Program to pass Parameter to a Thread

Samual Sam
Updated on 19-Jun-2020 11:47:31

3K+ Views

To work with threads, add the following namespace in your code −using System.Threading;Firstly, you need to create a new thread in C# −Thread thread = new Thread(threadDemo);Above, threadDemo is our thread function.Now pass a parameter to the thread −thread.Start(str);The parameter set above is −String str = "Hello World!";ExampleLet us see the complete code to pass a parameter to a thread in C#.Live Demousing System; using System.Threading; namespace Sample {    class Demo {       static void Main(string[] args) {          String str = "Hello World!";          // new thread       ... Read More

C# Program to Kill a Thread

karthikeya Boyini
Updated on 19-Jun-2020 11:48:19

684 Views

Create a thread first and start it −// new thread Thread thread = new Thread(c.display); thread.Start();Now display the thread and set a stop function to stop the working of the thread −public void display() {    while (!flag) {       Console.WriteLine("It's Working");       Thread.Sleep(2000);    } } public void Stop() {    flag = true;    } }ExampleThe following is the complete code to learn how to kill a thread in C#.Live Demousing System; using System.Threading.Tasks; using System.Threading; class Demo {        static void Main(string[] args){       MyClass c = new MyClass(); ... Read More

C# program to Illustrate Upper Triangular Matrix

Samual Sam
Updated on 19-Jun-2020 11:49:44

330 Views

For the upper triangular matrix, set all the elements below the main diagonal to zero.Set the following condition −if (i = j)                Console.Write(A[i, j] + "\t");             else                Console.Write("0\t");          }       }       Console.ReadLine();    } }OutputEnter number of rows and columns of the matrix   Enter elements:   Upper Triangular Matrix 

C# Program to Illustrate Lower Triangular Matrix

karthikeya Boyini
Updated on 19-Jun-2020 11:26:45

193 Views

For the lower triangular matrix, set all the elements above the main diagonal to zero.Set the following condition −if (i >= j)    Console.Write(A[i, j] + "\t"); else    Console.Write("0\t");ExampleYou can try to run the following code to display a lower triangular matrix.Live Demousing System; using System.Linq; class Demo {    static void Main() {       int m, n, i, j;       Console.Write("Enter number of rows and columns of the matrix ");       m = Convert.ToInt16(Console.ReadLine());       n = Convert.ToInt16(Console.ReadLine());       int[, ] A = new int[10, 10];     ... Read More

C# Program to implement Sleep Method Of Thread

Samual Sam
Updated on 19-Jun-2020 11:27:24

230 Views

The sleep method of the thread is used to pause the thread for a specific period.If you want to set sleep for some seconds, then use it like the following code snippet −int sleepfor = 2000; Thread.Sleep(sleepfor);ExampleYou can try to run the following code to implement the sleep method of the thread.Live Demousing System; using System.Threading; namespace MyApplication {    class ThreadCreationProgram {       public static void CallToChildThread() {          Console.WriteLine("Child thread starts");          int sleepfor = 2000;          Console.WriteLine("Child Thread Paused for {0} seconds", sleepfor / 1000);   ... Read More

C# program to find IP Address of the client

karthikeya Boyini
Updated on 19-Jun-2020 11:28:29

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

Advertisements