c#-2.0


How can i write anonymous method in my scenario


Let me tell you all that i am not getting any type of error in my code it is working perfectly fine, I am here to know one specific thing about Anonymous method. So firstly it is my code. I have a class called Employee. in which there are three auto implemented properties and a function which is returning the employees names who git bonus salary, I am using delegates so there is no logic in this function and that is the reason this class and method is reusable.
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public static void BonusForEmp(List<Employee> emp, Bonus Isavailable)
{
foreach (Employee e in emp)
{
if (Isavailable(e))
{
Console.WriteLine(e.Name + " got bonus");
}
}
}
}
delegate bool Bonus(Employee Employes); // Delegate
Here It Is my Main method in which i create a list of employees and doing all the steps like creating a instance and passing the method as an argument.
List<Employee> EmpList = new List<Employee>();
EmpList.Add(new Employee() { Id = 1, Name = "A", Salary = 12000});
EmpList.Add(new Employee() { Id = 2, Name = "B", Salary = 7999 });
EmpList.Add(new Employee() { Id = 3, Name = "C", Salary = 1999 });
EmpList.Add(new Employee() { Id = 4, Name = "D, Salary = 20000});
Bonus B = new Bonus(BonusAvailabeFor); //instance of dlegate and pasing method name as an argument
Employee.BonusForEmp(EmpList, B); //using
Console.ReadLine();
So All of you thinking that where is my method(BonusAvailableFor) in which i did all the logical work which matches the signature of delegate? Here it is
public static bool BonusAvailabeFor(Employee eml) // the function with same signature of the delegate here we use our logic.
{
if (eml.Salary > 8000)
{
return true;
}
else
{
return false;
}
}
So this code is working so perfectly nice and returning the names of employee whose Salary > 8000.
I just read anonymous method somewhere that is inline expression that can be used wherever a delegate type is expected.They provide us a way to create instance of delegate without having to write a separate method .. the definition looks great it will reduce my lines of code if i can implement this in my scenario, so how can i achieved it ? <=== (My Question)

Related Links

Please tell me something about multithreading in c# [closed]
Help With pattern matching
Problem With AND condition [closed]
where are C# setting file values are saved? it's not in the appName.config
Process.WaitForExit not waiting
Relative path on File.ReadAllLines method
Reading Excel using C# and OleDB - How to identify the used range?
Arraylist in Visual Studio .net 2005
Find duplicate values in an arraylist without using LINQ
C # Convert.ToDateTime() problem
why a protectd method of a class can nor be accessed by creating its object in sub class?
Do objects also have some scope like methods local variables have?
Some Logical OOP Concepts [closed]
How to download text file from server on local computer in C# framework 2.0.?
Correct way to compare two List<KeyValuePair<string, Object>> in C# 2.0
generation in garbage cllection

Categories

HOME
sendgrid
openmp
proxy
azure-data-factory
deezer
syntax
cplex
gis
mouse
basic
slick-slider
spring-kafka
etl
correlation
dtrace
visual-studio-cordova
vaadin7
telephony
foselasticabundle
propel
http-status-code-504
database-replication
crystal-reports-2008
textfield
conemu
vlsi
javacv
facebook-access-token
traffic
greendao
dbclient
delicious-api
frame
stormpath
uisplitview
airconsole
usbserial
normal-distribution
mapdb
wixsharp
lto
twitch
ws-security
bootstrap-dialog
dotcover
fancybox-2
np-complete
qtwebview
webdriverjs
jquery-filter
maven-tomcat-plugin
lib.web.mvc
interrupted-exception
operation
apache-commons-fileupload
census
skos
offloading
gadt
tablelayout
elliptic-curve
yorick
marmalade
xceed-datagrid
html5-notifications
multiprocessor
file-locking
random-seed
mqx
http-unit
cassini-dev
libstdc++
newtonscript
removeclass
eclipse-templates
actionview
iphone-web-app
subviews
icanhaz.js
adrotator
phonon
gacutil
defensive-programming
ubuntu-9.04

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App