Found 2587 Articles for Csharp

What are the differences between constructors and destructors in C#?

Ankith Reddy
Updated on 20-Jun-2020 12:57:24

525 Views

ConstructorsA class constructor is a special member function of a class that is executed whenever we create new objects of that class.A constructor has exactly the same name as that of class and it does not have any return type.Constructor has the same name as the class name −class Demo {    public Demo() {} }The following is an example −Example Live Demousing System; namespace LineApplication {    class Line {       private double length; // Length of a line       public Line() {          Console.WriteLine("Object is being created");   ... Read More

What are dynamic arrays in C#?

karthikeya Boyini
Updated on 20-Jun-2020 11:49:55

6K+ Views

Dynamic arrays are growable arrays and have an advantage over static arrays. This is because the size of an array is fixed.To create arrays dynamically in C#, use the ArrayList collection. It represents an ordered collection of an object that can be indexed individually. It also allows dynamic memory allocation, adding, searching and sorting items in the list.The following is an example showing how to create arrays dynamically in C# −Example Live Demousing System; using System.Collections; namespace Demo {    class Program {       static void Main(string[] args) {          ArrayList al = new ArrayList(); ... Read More

What are contextual keywords in C#?

Chandu yadav
Updated on 20-Jun-2020 11:50:16

723 Views

In C#, some identifiers have special meaning in context of code, such as get and set are called contextual keywords.The following is the table showing contextual keywords −Contextual Keywordsaddaliasascendingdescendingdynamicfromgetglobalgroupintojoinletorderbypartial (type)partial(method)removeselectset

What are constructors in C# programs?

Samual Sam
Updated on 20-Jun-2020 11:51:03

600 Views

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.A constructor has exactly the same name as that of class and it does not have any return type.Constructor has the same name as the class name −class Demo {    public Demo() {} }The following is an example −Example Live Demousing System; namespace LineApplication {    class Line {       private double length; // Length of a line       public Line() {          Console.WriteLine("Object is being created");     ... Read More

What are the comments in C#?

Arjun Thakur
Updated on 20-Jun-2020 11:57:09

178 Views

Comments are used for explaining code. Compilers ignore the comment entries. The multiline comments in C# programs start with /* and terminates with the characters */ as shown below.Multi-line comments/* The following is a mult-line comment In C# /*The /*...*/ is ignored by the compiler and it is put to add comments in the program.Single line comments// variable int a = 10;The following is a sample C# program showing how to add single-line as well as multi-line comments −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {       ... Read More

What are class instances in C#?

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

4K+ 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

267 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

159 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

775 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

4K+ 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.

Advertisements