Found 2628 Articles for Csharp

How to declare and use Interfaces in C#?

Samual Sam
Updated on 20-Jun-2020 10:24:46

123 Views

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

What are reserved keywords in C#?

karthikeya Boyini
Updated on 20-Jun-2020 10:25:34

2K+ Views

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

How to declare and instantiate Delegates in C#?

Samual Sam
Updated on 20-Jun-2020 10:26:30

249 Views

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

How to declare and initialize constant strings in C#?

karthikeya Boyini
Updated on 20-Jun-2020 10:27:16

9K+ Views

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

How to declare and initialize a list in C#?

Samual Sam
Updated on 20-Jun-2020 10:28:04

11K+ Views

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

How to declare and initialize a dictionary in C#?

karthikeya Boyini
Updated on 20-Jun-2020 10:28:45

1K+ Views

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To declare and initialize a Dictionary −IDictionary d = new Dictionary();Above, types of key and value are set while declaring a dictionary object. An int is a type of key and string is a type of value. Both will get stored in a dictionary object named d.Let us now see an example −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary d = new Dictionary();       d.Add(1, 97);       ... Read More

How to declare an event in C#?

Samual Sam
Updated on 20-Jun-2020 10:29:25

273 Views

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system-generated notifications.The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event.To declare an event inside a class, first a delegate type for the event must be declared. For example, public delegate string myDelegate(string str);Now, declare an event −event myDelegate newEvent;Let us see an example to work with events in C# −Example Live Demousing System; namespace Demo { ... Read More

How to write the first program in C#?

karthikeya Boyini
Updated on 20-Jun-2020 10:29:55

443 Views

The following is the first program in C# programming −Example Live Demousing System; namespace MyHelloWorldApplication {    class MyDemoClass {       static void Main(string[] args) {          // display text          Console.WriteLine("Hello World");          // display another text          Console.WriteLine("Welcome!");          Console.ReadKey();       }    } }OutputHello World Welcome!Let us see now what all it includes.using System; - the using keyword is used to include the System namespace in the program.namespace declaration - A namespace is a ... Read More

Iterators in C#

Samual Sam
Updated on 20-Jun-2020 10:30:53

285 Views

Iterator performs a custom iteration over a collection. It uses the yield return statement and returns each element one at a time. The iterator remembers the current location and in the next iteration the next element is returned.The following is an example −Example Live Demousing System; using System.Collections.Generic; using System.Linq; namespace Demo {    class Program {       public static IEnumerable display() {          int[] arr = new int[] {99, 45, 76};          foreach (var val in arr) {             yield return val.ToString();       ... Read More

Interfaces and Inheritance in C#

karthikeya Boyini
Updated on 20-Jun-2020 10:11:42

1K+ Views

InterfaceAn interface is defined as a syntactical contract that all the classes inheriting the interface should follow. The interface defines the 'what' part of the syntactical contract and the deriving classes define the 'how' part of the syntactical contract.Let us see an example of 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();    }    public class Transaction : ITransactions {       private string tCode;       ... Read More

Advertisements