Programming Articles - Page 3153 of 3363

What is default constructor in C# programs?

karthikeya Boyini
Updated on 20-Jun-2020 15:52:54

806 Views

A Constructor in C# is invoked automatically when an object gets created. The constructor has the same name as that of the class, for example, −public class Department {    public Department () {       Console.WriteLine("Default Constructor! ");    } }The following is the code that shows the usage of default constructor in C#. The constructor invokes immediately when the object gets created −Department dept1 = new Department ();A default constructor is a constructor that has no argument, for example −Department () { }Let us see the complete example to learn how to work with default constructors −Example Live ... Read More

What is composition in C#?

Arjun Thakur
Updated on 20-Jun-2020 15:54:17

591 Views

If the parent object is deleted under Composition, then the child object also loses its status. The composition is a special type of Aggregation and gives a part-of relationship.For example, A Car has an engine. If the car is destroyed, the engine is destroyed as well.public class Engine {    . . . } public class Car {       Engine eng = new Engine();       ....... }

What is a Ternary operator/conditional operator in C#?

Ankith Reddy
Updated on 20-Jun-2020 15:55:02

439 Views

Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.For example −y = (x == 1) ? 70 : 100;Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.The following is an example −Example Live Demousing System; namespace DEMO {    class Program {       static void Main(string[] args) {          int a, b;          a = 10;          b = (a == 1) ? 20 : 30;          Console.WriteLine("Value of b is {0}", b);          b = (a == 10) ? 20 : 30;          Console.WriteLine("Value of b is {0}", b);          Console.ReadLine();       }    } }OutputValue of b is 30 Value of b is 20

What is compile time polymorphism in C#?

Samual Sam
Updated on 20-Jun-2020 15:55:55

2K+ Views

Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.The linking of a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are Function overloading and Operator overloading.In function overloading you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument ... Read More

What are the methods and properties of the Thread class in C#?

George John
Updated on 20-Jun-2020 15:31:04

546 Views

Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems.The following are some of the properties of the Thread class −Sr.No.Property & Description1CurrentContextGets the current context in which the thread is executing.2CurrentCultureGets or sets the culture for the current thread.3CurrentPrincipleGets or sets the thread's current principal (for role-based security).4CurrentThreadGets the currently running thread.5CurrentUICultureGets or sets the current culture used by the Resource Manager to look up culture-specific resources at run-time.6ExecutionContextGets an ExecutionContext object that contains information about the various contexts of the current thread.7IsAliveGets a value indicating the execution status ... Read More

What are the main parts of a C# program?

Samual Sam
Updated on 20-Jun-2020 15:32:32

2K+ Views

The main parts of a C# program includes −Namespace declarationA classClass methodsClass attributesA Main methodStatements and ExpressionsCommentsThe following is an example showing how to create a C# program −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine("Our first program in C#!");          Console.ReadKey();       }    } }OutputOur first program in C#!Here are the parts of the C# program we saw above −using System; - the using keyword is used to include the System namespace in the program. A program ... Read More

What is method overloading in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:38:14

2K+ Views

Two or more than two methods having the same name but different parameters is what we call method overloading in C#.Method overloading in C# can be performed by changing the number of arguments and the data type of the arguments.Let’s say you have a function that prints multiplication of numbers, then our overloaded methods will have the same name but different number of arguments −public static int mulDisplay(int one, int two) { } public static int mulDisplay(int one, int two, int three) { } public static int mulDisplay(int one, int two, int three, int four) { }The following is an ... Read More

How to pass pointers as parameters to methods in C#?

Samual Sam
Updated on 20-Jun-2020 15:39:11

2K+ Views

To pass pointers as parameters to methods, refer the below steps −Firstly, crate a function swap with unsafe modifier.public unsafe void swap(int* p, int *q) {    int temp = *p;    *p = *q;    *q = temp; }Now under static void main, add the value for the first and second variable, set pointers for both of them.Display the values of the variables and then call the swap() method shown above. The method swaps the values and displays the result −public unsafe static void Main() {    Program p = new Program();    int var1 = 10;    int ... Read More

What are pointers in C#?

Arjun Thakur
Updated on 20-Jun-2020 15:38:39

6K+ Views

Pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location.The syntax of a pointer is −type *var-name;The following is how you can declare a pointer type −double *z; /* pointer to a double */C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable.The following is our module showing how to declare and use a pointer variable. We have used unsafe modifier here −static unsafe void ... Read More

How to pass parameters to a method in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:40:16

270 Views

To pass parameters to a method in C#, let us see how to pass parameters by value. In this mechanism, when a method is called, a new storage location is created for each value parameter.The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument.Here is the example showing how to pass parameters to a method −Example Live Demousing System; namespace Demo {    class NumberManipulator {       public void swap(int x, int y) {          int temp;     ... Read More

Advertisements