Академический Документы
Профессиональный Документы
Культура Документы
-------------------------
Action, Func and Predicate are pre-defined Generic delegates. So as delegate they can point to
functions with specified signature.
Action<>
Does not return any value, and can take from none up to 16 generic parameters. So there are total
of 17 overloads for Action delegate, from no
parameter to up to 16 generic parameters. As it does not return any value, so conventionally named
as Action.
- Action
- Action<T>
- Action<T1, T2>
- Action< ……
//Create an Action
moreAction("Hello", "World");
Func<>
Return a generic parameter, and has 17 overloads to support from none up to 16 generic
parameters. Last parameter is the Return type, and is
required. As it does return a parameter, so conventionally named as Func derived from Function.
- Func<TResult>
- Func<T1, TResult>
- Func< ……
Actual delegate definition of Func<T1, T2, out TResult> with two parameters would be:
Predicate<>
As name indicate, this delegate defines a method with return type bool, and exactly one generic
parameter. Method is pretty much used as a function
to affirm or deny certain input for specific operation. i.e Either provided input is true or false based
on specific method implementation. Mostly
return num % 2 == 0;
//Predicate
To save time from defining a delegates of similar definition yourself. Use the predefined one which
is also more commonly used, and well recognized.
With the invent of Extension methods and LINQ which introduced loads of functions with the
requirement of delegate parameter. A presence for general
-----------------------------------
The IEnumerable interface contains an abstract member function called GetEnumerator() and return
an interface IEnumerator on any success call. This
IEnumerator has two methods MoveNext and Reset. It also has a property called Current.
IEnumerable
IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.
While querying data from the database, IEnumerable executes "select query" on the server-side,
loads data in-memory on the client-side and then
list = list.Take<Employee>(10);
IQueryable
IQueryable is suitable for querying data from out-memory (like remote database, service)
collections.
While querying data from a database, IQueryable executes a "select query" on server-side with all
filters.
list = list.Take<Employee>(10);
Benefits of using Interfaces
----------------------------
You may feel that it is a waste of time to create interfaces but here is why it is important.
Readability, as an interface implements the signature of your code their name must make sense with
their purpose;
Maintainability, making it possible of having different implementations based on the same interface;
Extensibility, being easier to extend classes with a different implementation based on the same
contract.
C# Extension Method
--------------------
An extension method is a static method of a static class, where the "this" modifier is applied to the
first parameter. The type of the first
Extension methods are only in scope when you explicitly import the namespace into your source
code with a using directive.
Extension methods allow existing classes to be extended without relying on inheritance or having to
change the class's source code.
If the class is sealed than there in no concept of extending its functionality. For this a new concept is
introduced, in other words extension
methods.
This feature is important for all developers, especially if you would like to use the dynamism of the
C# enhancements in your class's design.
Important points for the use of extension methods
An extension method with the same name and signature as an instance method will not be called.
namespace ExtensionMethods
Extension method
using ExtensionMethods;
class Program
int i = 10;
Console.WriteLine(result);
}
Anonymous Method in C#
-----------------------
An anonymous method is a method which doesn’t contain any name which is introduced in C# 2.0. It
is useful when the user wants to create an inline
method and also wants to pass parameter in the anonymous method like other methods. An
Anonymous method is defined using the delegate keyword and the
using System;
Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous
methods are the methods without a name, just the body.
class GFG {
// Main method
mypet);
};
p("Dog");
}
}
lambda expression
-----------------
A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ.
Simply put, it's a method without a declaration,
// Returns 35
// Returns 35
---------------------------------------------
The out is a keyword in C# which is used for the passing the arguments to methods as a reference
type. It is generally used when a method returns
multiple values. The out parameter does not pass the property.
The ref is a keyword in C# which is used for the passing the arguments by a reference. Or we can say
that if any changes made in this argument in the
method will reflect in that variable when the control return to the calling method. The ref parameter
does not pass the property.
------------------------------------------------------
Non-static classes can be instantiated, whereas static classes cannot be instantiated i.e. you cannot
use the new keyword to create a variable of the
class type
ou access the members of a static class by using the class name itself
----------------------------------------------------------------------
A private constructor is used in classes containing only static member as shown below −
class Demo {
// private constructor
private Demo() { }
A singleton class has normal methods and you can call it using an instance.
private Singleton() {
-------------------------------------------
Private: Private limits the visiblity to a scope. Declaring a private class within a class means that "sub-
class" can't be seen from outside of the
class.
This is also true for methods and properties - they can be seen within the class, but not to any
consumers or inheritors.
Sealed: If a class is declared as sealed, that means that you cannot inherit from the class. Sealed class
can be used when a class is internal to the
operation of the library, class or whwn you donot want that class to be overridden because it may
affect the functionality.
------------------------------------------
Virtual
The virtual keyword allows a class to be overridden. For overriding a parent class method in the child
class, declare the parent class method as
virtual.
Sealed
When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
To prevent being overridden, use the sealed in C#. When you use sealed modifiers in C# on a
method, then the method loses its capabilities of
overriding. The sealed method should be part of a derived class and the method must be an
overridden method.
New
Use the new keyword to hide the base class method from the derived class. This is done by declaring
the derived class function with new. This is how
Abstract
Abstract classes contain abstract methods, which are implemented by the derived class.
------------------------
Handling child controls events through parent control in data bind controls known as event bubbling.
That means, when events occurred in child
controls , the event is bubbled to the parent control. A control can participate in event bubbling
through two methods that it inherits from the base
the OnBubbleEvent method. RaiseBubbleEvent sends the event data up the hierarchy to the
control's parent.
Event Bubbling is nothing but events raised by child controls is handled by the parent control.
Example: Suppose consider grid view as parent control
in which there are several child controls.There can be a column of link buttons right.Each link button
has click event.Instead of writing event
routine for each link button write one routine for parent which will handle the click events of the
child link button events.
-------------------
Events:
Event is a mechanism by which a class can send notification to its client. For example, you have an
application to perform certain operation, if an
operation is failed then the application send the notification to log file, printer, fax, email etc.
Events are declared using delegates. Without delegate, we can not create Events.
Delegate:
A delegate in C# is similar to a function pointer in C. A delegate object holds the reference of one or
more methods. Delegate creation is the four
steps process.
Declaration of delegate
Invoke delegate
A delegate object doesn't care about the class in which the function exists. There is only one
restriction that the delegate signature and the
function signature should be same otherwise delegate object can’t hold the reference of the method
which doesn't have the same signature. Signature
namespace SampleApp {
class EventProgram {
public EventProgram() {
Console.WriteLine(result);
---------------------------------------------
The function which you want to do overload must be a virtual function. Otherwise, it will not
overload and overwrite.
Exception handling
------------------
C# Exception handling is a mechanism in .NET to detect and handle run time errors. The try..catch
block is used to implement exception handling in
C#. In try..catch..finally block, finally is used for code cleanup. Code sample for multiple try catch
block.
try..catch..finally
C# provides three keywords try, catch and finally to implement exception handling. The try encloses
the statements that might throw an exception
whereas catch handles an exception if one exists. The finally can be used for any cleanup work that
needs to be done.
try
catch(Type x)
finally
Throwing an Exception
In C#, it is possible to throw an exception programmatically. The 'throw' keyword is used for this
purpose. The general form of throwing an exception
is as follows.
throw exception_obj;
using System;
class MyClient
try
catch (DivideByZeroException)
Console.WriteLine("Exception");
Console.WriteLine("LAST STATEMENT");
Re-throwing an Exception
The exceptions, which we caught inside a catch block, can re-throw to a higher context by using the
keyword throw inside the catch block. The
class MyClass
try
int x = 0;
catch (DivideByZeroException)
throw;
class MyClient
try
mc.Method();
catch (Exception)
Console.WriteLine("LAST STATEMENT");
}
}
Standard Exceptions
System.OutOfMemoryException
System.NullReferenceException
Syste.InvalidCastException
Syste.ArrayTypeMismatchException
System.IndexOutOfRangeException
System.ArithmeticException
System.DevideByZeroException
System.OverFlowException
User-defined Exceptions
In C#, it is possible to create our own exception class. But Exception must be the ultimate base class
for all exceptions in C#. So the user-defined
exception classes must inherit from either Exception class or one of its standard derived classes.
using System;
class MyClient
{
try
catch (Exception)
Console.WriteLine("LAST STATEMENT");
----------------------------
finally – The finally block always executes when the try block exits, except System.exit(0) call. This
ensures that the finally block is executed
finalize() – method helps in garbage collection. A method that is invoked before an object is
discarded by the garbage collector, allowing it to