Found 34485 Articles for Programming

How to declare variables in C#?

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

239 Views

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 declare variables − ;Let us see an example to declare two integer variables −int a, b;Above the variable is of int type. Let us declare a variable for other types −Variable of float type.float f;Variable of double type.double d;Let us display a variable −Example Live Demousing System; using System.Collections; class Demo {    static void Main() {     ... Read More

How to declare member function in C# interface?

karthikeya Boyini
Updated on 20-Jun-2020 10:22:19

149 Views

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    } }

How to declare and use Interfaces in C#?

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

126 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

251 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

275 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

447 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

Advertisements