Home
Feedback
FAQ
Getting Started
Discussions
Site operation discussions
Recent Posts
(new topic)
Departments
Courses
Research Papers
Design Docs
Quotations
Genealogical Diagrams
Archives
Mentioned on OS News. The features mentioned in the C# 3.0 preview, if nothing else, will serve to distance the product from Java still further. Lambda and query expressions...hmmm...
Extension Methods a la SmallTalk
Instead of making a string class of your own, you can extend it without inheritance or composition. The method "Chomp" does not exist in .NET string type.
----------Extensions.cs------------- namespace MyCompany.StringUtilities { public static class Extensions { public static int Chomp(this string s) { return s.EndsWith("\n") ? s.TrimEnd("\n".ToCharArray()) : s; } } } -------MyClass.cs------------ using MyCompany.StringUtilities; class MyClass { public static void Main() { string s1 = "test\n"; string s2 = s1.Chomp(); } }
Lambda Expressions As taken from the Language Specifications:
x => x + 1 // Implicitly typed, expression body x => { return x + 1; } // Implicitly typed, statement body (int x) => x + 1 // Explicitly typed, expression body (int x) => { return x + 1; } // Explicitly typed, statement body (x, y) => x * y // Multiple parameters () => Console.WriteLine() // No parameters myObject.MyMethodTakingDelegateParam( x => x + 1);
Type Inference
int i; // you can still use type annotations var i = 0; // type inferred as int var name = "dude"; // type inferred as string; var question = new[] { "dude", "where's", "my", "car" }; // type inferred string[] array
Anonymous Types
var person = new { Name = "dude", Weight = 150 } var employees = new[] { new { Name = "Chris Smith", PhoneNumbers = new[] { "206-555-0101", "425-882-8080" } }, new { Name = "Bob Harris", PhoneNumbers = new[] { "650-555-0199" } } };
Looking at your first example about "Extension Methods" I wonder if the method is really added to the string class prototype (and in that case how does it types in other parts of program, and what about collisions when two programs are adding the same method ?)
Or is it just syntactic sugar and the s1.Chomp() will translate at compilation time to Chomp(s1) ? That would make more sense but in that case how can you share your extensions among several parts of the code ?
I re-expanded the example.
To answer your question, I quote the spec
...In effect, imported extension methods appear as additional methods on the types that are given by their first parameter and have lower precedence than regular instance methods...
Regarding the line: int Chomp(this string s) Is "this" really needed?
Wouldn't it be easier just to allow the following: int Chomp(string s) //usual method signature
Name spaces will take care of method name collisions?
In Dylan, a function call x.f is syntactic sugar for f(x). (I think it also works for multi-argument functions, but I don't recall.)
The actual mention on OSNews (titled "Nemerle 0.9.0 released") is getting harder to track down.
Falcon, if you don't include the "this" keyword, how can C# tell that you want MyCompany.StringUtilities.Extensions.Chomp() to be an extension method instead of a normal method in the Extensions class?
The "this" keyword is what signals to C# 3.0 that this is an extension method. This is logical since the string "s" becomes the hidden "this" parameter when calling Chomp().
Recent comments
27 weeks 2 days ago
27 weeks 2 days ago
27 weeks 2 days ago
49 weeks 3 days ago
1 year 1 week ago
1 year 3 weeks ago
1 year 3 weeks ago
1 year 5 weeks ago
1 year 10 weeks ago
1 year 10 weeks ago