Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: C# guys, show us your sugar!

  1. #1
    Join Date
    Nov 2007
    Location
    New Zealand
    Beans
    1,026
    Distro
    Ubuntu 11.04 Natty Narwhal

    C# guys, show us your sugar!

    Im hoping that there are some good C# guys out there that can help me with this thread. Basically I want to demonstrate two things, C# is different from Java (its actually better) and C# has learnt something from dynamic languages.

    This is not an ignorant “I hate M$” thread. This an informative thread that aims to dispel some of the common misconceptions about C# and also a good opportunity for you C# guys to show off (please do!!!).

    Now, I can start this off but because I have been somewhat removed from the C# dev world im going to need some help.

    C# basically used to be the same as Java back when I was developing with it (Version 1 of the language spec). But since I developed with it there have been 2 further spec updates that I think have yielded some nice syntax sugar for the language.

    Some of the most notable for me are...

    Type Inference:
    var list = new List<string>();

    Automatic properties:
    This is a great time saver, no more being a monkey and writing getters and setters code.

    Object initializers:
    Person p = new Person { Name="tinny" };

    Collection initializers:
    MyList list = new MyList { 1, 2 };

    Anonymous types:
    Hack together a type on the fly
    var x = new { Name = "tinny" }

    Im sure this is just the tip of the iceberg, I know for a fact that im not the only one wanting to learn more about the best parts of C#.

    So, C# guys show us your sugar!

  2. #2
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: C# guys, show us your sugar!

    Quote Originally Posted by tinny View Post
    Object initializers:
    Person p = new Person { Name="tinny" };
    You can do something very similar in Java.

    PHP Code:
    Person p = new Person() {{
       
    this.name "tinny";
       
    this.age 19;
    }}; 
    Means that this also can be done:

    PHP Code:
    List list = new ArrayList() {{
       
    this.add("first");
       
    this.add("second");
    }}; 
    It's a hack but...


    Things supernice in C# are delegates and anonymous delegates (example from wikipedia):

    PHP Code:
    public void Foo(object parameter) {
        
    // ...
     
        
    ThreadPool.QueueUserWorkItem(delegate
        
    {
            
    // anonymous delegates have full access to local variables of the enclosing method
            
    if (parameter == ...)
            { 
                
    // ... 
            
    }
     
            
    // ...
        
    });

    extesion methods (example from wikipedia):
    PHP Code:
    public static class IntExtensions
    {
        public static 
    void PrintPlusOne(this int x) { Console.WriteLine(1); }
    }
     
    int foo 0;
    foo.PrintPlusOne(); 
    python like iterators, generators(example from wikipedia):
    PHP Code:
    public static IEnumerable<intGetEven(IEnumerable<intnumbers)
    {
        foreach (
    int i in numbers)
        {
            if (
    == 0) yield return i;
        }

    and generics without erasure.

    Edit:
    Oh.. and I forgot one very nice thing - operator overloading!
    Last edited by Quikee; October 6th, 2008 at 10:11 AM. Reason: Forgot something..

  3. #3
    Join Date
    Nov 2007
    Location
    SLC
    Beans
    200
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: C# guys, show us your sugar!

    In regards to C# being better than JAVA: Do you write C# applications for linux?

  4. #4
    Join Date
    Nov 2007
    Location
    New Zealand
    Beans
    1,026
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C# guys, show us your sugar!

    Quote Originally Posted by davidryder View Post
    In regards to C# being better than JAVA: Do you write C# applications for linux?
    I know what you are trying to get at.

    The OP mentioned the C# language, not .Net / Mono (mono is a .Net port for linux ) vs Java platform. The Java platform is another story...

    </offtopic>
    Last edited by tinny; October 6th, 2008 at 10:12 AM.

  5. #5
    Join Date
    Nov 2007
    Location
    New Zealand
    Beans
    1,026
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: C# guys, show us your sugar!

    Quote Originally Posted by Quikee View Post
    Oh.. and I forgot one very nice thing - operator overloading!
    http://www.csharphelp.com/archives/archive135.html

    Cool, I must say I am a fan!


    C# Closures

    http://srtsolutions.com/blogs/billwa...-closures.aspx


    Closures are coming in Java 7, I cant believe it has taken that long to get them! I am constantly left begging for Closures, I dont really like the anonymous inner class approach (the way you do it in Java right now) and tend just to live without pretend Closures in Java (smells up the code IMO).

  6. #6
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: C# guys, show us your sugar!

    Quote Originally Posted by tinny View Post
    C# Closures

    http://srtsolutions.com/blogs/billwa...-closures.aspx


    Closures are coming in Java 7, I cant believe it has taken that long to get them! I am constantly left begging for Closures, I dont really like the anonymous inner class approach (the way you do it in Java right now) and tend just to live without pretend Closures in Java (smells up the code IMO).
    Yes closures are also supernice - but if Java would have a "simpler" syntax to instantiate anonymous classes would already be a lot nicer. Most of the syntactic sugar that is normally solved with closures can be solved with anonymous classes.

  7. #7
    Join Date
    May 2006
    Location
    Pakistan
    Beans
    425
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: C# guys, show us your sugar!

    Great thread, I'd like to read more things about C#, and it will increase my knowlege as a C# beginner.

  8. #8
    Join Date
    Jul 2008
    Beans
    1,706

    Re: C# guys, show us your sugar!

    i learned to program in C# but stopped due to lack of tutorials and books for game programming

    i still think its a good language and has better error handling (or maybe i just do it wrong...) then java

  9. #9
    Join Date
    Sep 2007
    Beans
    Hidden!

    Re: C# guys, show us your sugar!

    i learned to program in C# but stopped due to lack of tutorials and books for game programming
    Disagreed.
    Books from Amazon related to C# game development.
    Google->tutorials
    That's not a reason to stop/start learning a language in my opinion.

  10. #10
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: C# guys, show us your sugar!

    Quote Originally Posted by Quikee View Post
    Most of the syntactic sugar that is normally solved with closures can be solved with anonymous classes.
    The anonymous inner class approach in Java is seriously limited though by the weird "final" requirement for the "closure member variables". Writing a loop around an anonymous inner class with some loop counter inside a final wrapper, for example, because really ugly really quickly

    That said, I am so spoiled by languages that do have closures that my Java is full of instantiations of my Callback<T> and is probably really hard to read for someone who isn't used to the style
    LambdaGrok. | #ubuntu-programming on FreeNode

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •