Found 2628 Articles for Csharp

What are class instances in C#?

Arjun Thakur
Updated on 20-Jun-2020 12:45:18

3K+ Views

Class instances are objects. Like any other object-oriented language, C# also has object and classes. Objest are real-world entities and instance of a class. Access the members of the class using an object.To access the 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 for example, Box Box1 = new Box();Above you can see Box1 is our object. We will use it to access the members −Box1.height = 7.0;You can also use it to call member functions −Box1.getVolume();The following is an example ... Read More

What is the default constructor in C#?

Samual Sam
Updated on 20-Jun-2020 12:46:36

166 Views

A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A default constructor does not have any parameter.The following is an example showing how to work with default constructor in C# −Example Live Demousing System; namespace LineApplication {    class Line {       private double length; // Length of a line       public Line(double len) { //Parameterized constructor       Console.WriteLine("Object is being created, length = {0}", len);          length = len;       }     ... Read More

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

515 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

Advertisements