Properties In C# with examples

In this article, we are going to talk about the use of properties in C# with examples.

What are the Properties in C#

A Property in C# is an extension of a field that provides a public way for the classes to read, write, and expose the private fields while hiding the implementation details.

Properties in C# are actually a special kind of getter and setter methods wrapped around a private variable. A  get  property accessor is used to retrieve the value and the  set  accessor is used to assign the new value.

( get : Retrieve  set : Assign)                  

C# Properties
Properties in C#

Property Syntax in C#

Continue reading “Properties In C# with examples”

var keyword in C#-Restrictions and Advantage of using var variable

In this article, we will understand the var keyword in C#.

var keyword in C#

The  var  keyword was introduced in C# 3.0 to declare an implicitly typed of local variable to hold any type of data.
In implicitly typed variables, the type of the variable declared is decided by the compiler at the compile-time based on the type of the value that the variable is initialized with.
The variable declared as  var  is mandatory to initialize at the time of declaration.

var keyword in C#
var variable in C#

The following are the implicitly and explicitly typed variables declaration in C#.

Continue reading “var keyword in C#-Restrictions and Advantage of using var variable”

Generic Delegates in C# With Examples

In the previous article, we have learned about the delegates and their types. Now in this article, we are going to discuss the generic delegates in C#.

What are the generic delegates?

The  Func ,  Action  and  Predicate  are the three generic delegates which were introduced in .NET 3.5 and are availables in System namespace.

We can make use of all these three predefined generic delegates when we want to invoke the methods without defining the delegates explicitly.

Func delegate in C#

Func generic delegates in c#

Func is a generic delegate that is available in the System namespace.

It can take up to 16 input parameters of the same or different data types and one out parameter.

Continue reading “Generic Delegates in C# With Examples”

C# – Anonymous Method with examples

What is Anonymous Method?

 Anonymous method  : In C#, An anonymous method is a method without name and return type.

It can be created using the delegate keyword with the optional parameters and can be assigned to a variable of the delegate type.
An anonymous method is an inline statement or expression which makes the code easy and more readable.

An anonymous method is useful when we want to hook an action directly to an event without separately creating an event handler.

Syntax to declare an anonymous method

The following is the syntax to declare an anonymous method in C#.

delegate(Parameters)
{
    // Code
};

Continue reading “C# – Anonymous Method with examples”

What is a delegate in C#? Multicast Delegate with Examples

delegate in c#

C# Delegates with Examples

In C#, a  Delegate  is a reference type that is available in system namespace.

A delegate is a wrapper of a function, or simply we can say that it is a Typesafe function pointer which can hold the reference of one or more methods of the same signature and return type.

Syntax to define a delegate in C#

In C#, The declaration of a delegate is very similar to a method signature. It can also have parameters and the return type as a normal method.

Following is the syntax to declare a delegate.

<AccessModifier> delegate <ReturnType> <DelegateName>(<Parameters>)

Continue reading “What is a delegate in C#? Multicast Delegate with Examples”

Page 1 of 4
1 2 3 4