Found 34492 Articles for Programming

How to use an assignment operator in C#?

karthikeya Boyini
Updated on 20-Jun-2020 11:39:47

92 Views

Assign value to a variable using the assignment operator in C# −The following are the assignment operators in C# −OperatorDescriptionExample=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B assigns value of A + B into C+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C – A*=Multiply ... Read More

How to concatenate two strings in C#?

George John
Updated on 20-Jun-2020 11:40:10

684 Views

To concatenate two strings, use the String.Concat method.Let’s say you want to concatenate two strings in C#, str1 and str2, then add it as arguments in the Concat method −string str3 = string.Concat(str1, str2);ExampleThe following is the example − Live Demousing System; class Program {    static void Main() {       string str1 = "Brad";       string str2 = "Pitt";       // Concat strings       string str3 = string.Concat(str1, str2);       Console.WriteLine(str3);    } }OutputBradPitt

How to capture divide by zero exception in C#?

Samual Sam
Updated on 20-Jun-2020 11:40:58

3K+ Views

System.DivideByZeroException is a class that handles errors generated from dividing a dividend with zero.ExampleLet us see an example −using System; namespace ErrorHandlingApplication {    class DivNumbers {       int result;       DivNumbers() {          result = 0;       }       public void division(int num1, int num2) {          try {             result = num1 / num2;          } catch (DivideByZeroException e) {             Console.WriteLine("Exception caught: {0}", e);          } finally {             Console.WriteLine("Result: {0}", result);          }       }       static void Main(string[] args) {          DivNumbers d = new DivNumbers();          d.division(25, 0);          Console.ReadKey();       }    } }OutputThe values entered here is num1/ num2 −result = num1 / num2;Above, if num2 is set to 0, then the DivideByZeroException is caught since we have handled exception above.

How to calculate the length of the string using C#?

Ankith Reddy
Updated on 20-Jun-2020 11:42:42

10K+ Views

Use the String.Length property in C# to get the length of the string.str.LengthThe property calculates the words in the string and displays the length of the specified string, for example, the string Amit has 4 characters −string str = "Amit";ExampleThe following is the C# program to calculate the string length − Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Amit";          Console.WriteLine("String: "+str);          Console.WriteLine("String Length: "+str.Length);          Console.ReadKey();       }    } }OutputString: Amit String Length: 4

How to call a method of a class in C#

karthikeya Boyini
Updated on 20-Jun-2020 11:43:43

2K+ Views

To call a method, use the name of the method after the object name, for example, −obj1. Display();Let’s say the class name is ApplicationOne, so to call the method −ApplicationOne one = new ApplicationOne(); //calling the displayMax method ret = one.displayMax(a, b);The following is the example showing how to call a method in C# −Example Live Demousing System; namespace Demp {    class ApplicationOne {       public int displayMax(int num1, int num2) {          /* local variable declaration */          int result;          if (num1 > num2) ... Read More

How to use Null Coalescing Operator (??) in C#?

Samual Sam
Updated on 20-Jun-2020 11:48:30

2K+ Views

The null coalescing operator is used with the nullable value types and reference types. It is used for converting an operand to the type of another nullable (or not) value type operand, where an implicit conversion is possible.If the value of the first operand is null, then the operator returns the value of the second operand, otherwise, it returns the value of the first operand.The following is an example −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          double? num1 = null;     ... Read More

How to assign multiple values to same variable in C#?

Chandu yadav
Updated on 20-Jun-2020 11:21:17

3K+ Views

To set multiple values to same variable, use arrays in C#. Let’s say instead of taking 5 variables, set these 5 variables using arrays in a single variable.The following is an example to set three values to a single variable with a string array −string[] arr = new string[3];Let us now initialize it −string[] arr = new string[3] {"one", "two", "three"};The following is the complete example −Example Live Demousing System; public class Demo {    static void Main(string[] args) {       string[] arr = new string[3] {"one", "two", "three"};       for (int i = ... Read More

How to assign values to variables in C#?

karthikeya Boyini
Updated on 20-Jun-2020 11:31:36

517 Views

A variable is a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.To assign values to a variable, add the value after the equal operator −int a = 10;Let us see how to assign values to a variable and print itExample Live Demousing System; namespace Demo {    class Program {       static ... Read More

How to define multiline String Literal in C#?

George John
Updated on 20-Jun-2020 11:32:09

923 Views

Let’s say the string is −Welcome User, Kindly wait for the image to loadFor multiline String Literal, firstly set it like the following statement using@ prefix −string str = @"Welcome User, Kindly wait for the image to load";Now let us display the result. The string is a multi-line string now −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          string str = @"Welcome User,          Kindly wait for the image to          load";          Console.WriteLine(str);       }    } }OutputWelcome User, Kindly wait for the image to load

How to define multi-dimensional arrays in C#?

karthikeya Boyini
Updated on 20-Jun-2020 11:33:35

231 Views

C# allows multidimensional arrays. It includes an array with more than one dimension. Declare a 2-dimensional array of strings as −string [, ] names;A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns.Multidimensional arrays may be initialized by specifying bracketed values for each row. The following array is with 4 rows and each row has 4 columns.int [, ] a = new int [4, 4] { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed ... Read More

Advertisements