What is difference between anonymous methods and anonymous types in c# [Resolved]

Posted by Allemahesh under C# on 8/19/2013 | Points: 10 | Views : 6408 | Status : [Member] [MVP] | Replies : 3
What is difference between anonymous methods and anonymous types in c#




Responses

Posted by: Satyapriyanayak on: 8/19/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler

The concept of anonymous method was introduced in C# 2.0. An anonymous method is inline unnamed method in the code. It is created using the delegate keyword and doesn’t required name and return type. Hence we can say, an anonymous method has only body without name, optional parameters and return type. An anonymous method behaves like a regular method and allows us to write inline code in place of explicitly named methods.
Features of anonymous method

A variable, declared outside the anonymous method can be accessed inside the anonymous method.

A variable, declared inside the anonymous method can’t be accessed outside the anonymous method.

We use anonymous method in event handling.

An anonymous method, declared without parenthesis can be assigned to a delegate with any signature.

Unsafe code can’t be accessed within an anonymous method.

An anonymous method can’t access the ref or out parameters of an outer scope.

Assign an Anonymous Method to a Delegate

delegate int MathOp(int a, int b);
public static void Main() { //statements
MathOp op = delegate(int a, int b) { return a + b; };
int result = MathOp(13, 14);
//statements
}

Anonymous Method as an Event Handler

<form id="form1" runat="server">
<div align="center">
<h2>Anonymous Method Example</h2>
<br />
<asp:Label ID="lblmsg" runat="server" ForeColor="Green" Font-Bold="true"></asp:Label>
<br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</div>
</form>

http://msdn.microsoft.com/en-us/library/bb397696%28v=vs.90%29.aspx
http://www.dotnet-tricks.com/Tutorial/csharp/40ID180612-C-Sharp-Anonymous-Method.html

If this post helps you mark it as answer
Thanks

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ssj_Kumar on: 8/19/2013 [Member] Starter | Points: 25

Up
0
Down
below link will solve your question
http://stackoverflow.com/questions/208381/whats-the-difference-between-anonymous-methods-c-2-0-and-lambda-expressions
http://www.codeproject.com/Articles/47887/C-Delegates-Anonymous-Methods-and-Lambda-Expressio

Regards,
Jayakumar Selvakani

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/19/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Jayakumar,
Please check the links before posting on site.

The First link is
http://stackoverflow.com/questions/208381/whats-the-difference-between-anonymous-methods-c-2-0-and-lambda-expressions
This is showing that difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)

The second link is
http://www.codeproject.com/Articles/47887/C-Delegates-Anonymous-Methods-and-Lambda-Expressio
This is also showing the Delegates, Anonymous Methods, and Lambda Expressions.

My question is not related to theses link.
Please read the questions before posting the answer.


Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response