To declare and initialize a list in C#, firstly declare the list −List myList = new List()Now add elements −List myList = new List() { "one", "two", "three", };Through this, we added six elements above.The following is the complete code to declare and initialize a list in C# −Example Live Demousing System; using System.Collections.Generic; class Program { static void Main() { // Initializing collections List myList = new List() { "one", "two", "three", "four", "five", "size" }; Console.WriteLine(myList.Count); } }Output6
To set a constant in C#, use the const keyword. Once you have initialized the constant, on changing it will lead to an error.Let’s declare and initialize a constant string −const string one= "Amit";Now you cannot modify the string one because it is set as constant.Let us see an example wherein we have three constant strings. We cannot modify it after declaring −Example Live Demousing System; class Demo { const string one= "Amit"; static void Main() { // displaying first constant string Console.WriteLine(one); const string two = ... Read More
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Syntax for declaring Delegates −delegate Let us now see how to instantiate delegates in C#.Once a delegate type is declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the new expression is written similar to a method call, but without the arguments to the method.public delegate void printString(string s); ... Read More
Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. If you want to use these keywords as identifiers, you may prefix the keyword with the @ character.In C#, some identifiers have special meaning in the context of code, such as get and set are called contextual keywords.The following table lists the reserved keywords −abstractAsbaseboolbreakbytecasecatchCharcheckedclassconstcontinuedecimaldefaultdelegatedodoubleelseenumeventexplicitExternfalsefinallyfixedfloatforforeachGotoifimplicitinin (generic modifier)intinterfaceinternalislocklongnamespacenewnullObjectoperatoroutout (generic modifier)overrideparamsprivateprotectedpublicreadonlyrefreturnsbytesealedShortsizeofstackallocstaticstringstructswitchThisthrowtruetrytypeofuintulonguncheckedunsafeushortusingvirtualvoidvolatileWhileLet us see an example of using bool reserved keyword in C# −Example Live Demousing System; using System.Collections; class Demo { static void Main() { bool[] arr = new bool[5]; ... Read More
Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members.Let us declare interfaces −public interface ITransactions { // interface members void showTransaction(); double getAmount(); }The following is an example showing how to declare and use Interfaces in C# −Example Live Demousing System.Collections.Generic; using System.Linq; using System.Text; using System; namespace InterfaceApplication { public interface ITransactions { // interface members void showTransaction(); double getAmount(); ... Read More
To declare member functions in interfaces in C# −public interface InterfaceName { // interface members void InterfaceMemberOne(); double InterfaceMembeTwo(); void InterfaceMemberThree() } public class ClassName: InterfaceName { void InterfaceMemberOne() { // interface member } }Above we saw our interface members are −void InterfaceMemberOne(); double InterfaceMembeTwo(); void InterfaceMemberThree()We have then used it in the class for implementing interface −public class ClassName: InterfaceName { void InterfaceMemberOne() { // interface member } }
A delegate in C# is a reference to the method. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.Let us see how to declare delegates in C#.delegate Let us see an example to learn how to work with Delegates in C#.Example Live Demousing System; using System.IO; namespace DelegateAppl { class PrintString { static FileStream fs; static StreamWriter sw; ... Read More
Like any other programming language, in C#, you can easily create a user-defined exception. User-defined exception classes are derived from the Exception class. Custom exceptions are what we call user-defined exceptions.In the below example, the exception created is not a built-in exception; it is a custom exception −TempIsZeroExceptionYou can try to run the following code to learn how to create a user-defined exception in C#.Example Live Demousing System; namespace Demo { class TestTemperature { static void Main(string[] args) { Temperature temp = new Temperature(); try { ... Read More
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope.It has exactly the same name as that of the class with a prefixed tilde (~), for example, our class name is Demo.public Demo() { // constructor Console.WriteLine("Object is being created"); } ~Demo() { //destructor Console.WriteLine("Object is being deleted"); }Let us see an example to learn how to work with Destructor in C#.Example Live Demousing System; namespace LineApplication { class Line { private double length; // Length of a line ... Read More
When a method with parameters is called, you need to pass the parameters to the method using any of the following three methods -Reference ParametersThis method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.Value ParametersThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.In Value parameters, when a method is called, a new storage location is created for each value ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP