Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Learning c#, .NET programming

  1. Jul 30, 2007 #1

    Gza

    User Avatar

    Hi all, I was wondering whether anyone here had any experience with c# (and .NET programming in general) and why it is so much better than c++/java/VB (this is according to my compsci friend who is making some pretty serious money as an ASP.NET developer) Anyone know some good tutorials for a person transitioning from c++ (5 yrs experience)?
     
  2. jcsd
  3. Jul 30, 2007 #2

    -Job-

    User Avatar
    Science Advisor

    I wouldn't say it's better than c++ because c++ and c# have different strengths. C# is very much like Java, so it's a tough argument there as well, but personally i prefer C#.
    .NET 3.0 adds support for lambda expressions and LINQ which enables you to do SQL like queries on arrays and collections. It also supports DLING and XLing for tying classes with sql and xml schemas.
    You also have more control over garbage collection. You can pass parameters by reference (by using ref or out).
    In C# you can define attributes, which enable you to hide getters and setters. For example, while in java you might have:
    Code (Text):

    public class MyCart{
        int unitPrice, numberOfUnits;
        public int GetAmount(){
            return unitPrice * numberOfUnits;
        }
        public int SetAmount(int value){
           unitPrice = (int)(value/numberOfUnits);
        }
    }
     
    In c# you can do:
    Code (Text):

    public class MyCart{
    int unitPrice, numberOfUnits;
        public int Amount{
            get{ return unitPrice * numberOfUnits; }
            set{  unitPrice = (int)(value/numberOfUnits); }
        }
    }
     
    So you don't have so many visible Getters and Setters, the functionality is the same, but has a natural syntax.

    You can also define indexed attributes. For example:
    Code (Text):

    public class MyObject{
        public object this[string prop] {
            get{
                //for example we can query a database to get the property
                SqlConnection conn = new SqlConnection(...);
                SqlCommand comm = new Command("SELECT " + prop + " from MyTable WHERE filter = value", conn);
                object o = comm.ExecuteScalar();
                conn.Close();
                return o;
            }
        }
    }
    //usage
    MyObject o = new MyObject();
    Response.Write(o["FirstName"]);
     
    You have foreach/in loops, which enable you to replace:
    Code (Text):

    String[] ss = new String[]{ "a", "b", "c" };
    for(int i=0; i<ss.length(); i++){
         doSomething(ss[i]);
    }
     
    With:
    Code (Text):

    string[] ss = new string[]{ "a", "b", "c" };
    foreach(string s in ss){
        doSomething(s);
    }
     
    Goto is supported.
    You can also use lock() for thread synchronization, i.e.:
    Code (Text):

    public static object MySharedVariable = new object();
    public void DoWork(){
        lock(MySharedVariable){
           //read or write shared variable safely  
        }
    }
     
    Then you have delegates and events. You can use += and -= to register and unregister event handlers. With delegates you can write extensible classes. For example, you do a custom search control which does automatic paging of the results, but you want to let the programmer build the database search query according to his needs. You can define a delegate:
    Code (Text):

    public delegate string[] DoSearch(string[] terms);
    public DoSearch Search;
     
    And your class will call Search() to obtain the search results. So the prgrammer can use your control in the following manner:
    Code (Text):

    public MyClass{
        SearchControl sc = new SearchControl();
        sc.Search = new DoSearch(this.MySearch);
        myWindow.Controls.Add(sc);
    }
    public string[] MySearch(string[] terms){
        //search here
        return results;
    }
     
    Anonymous delegates are only different in that they don't need to be members of a class i.e.:
    Code (Text):

    SomeDelegate del = delegate                        
                             {
                                 MessageBox.Show("Hello");
                             };
    del();
     
    There are also partial types, generic collections (as in Java) and generic delegates. A bunch of stuff really.
     
    Last edited: Jul 30, 2007
  4. Jul 31, 2007 #3

    Gza

    User Avatar

    This was an excellent intro, thank you very much. Any good books/resources you recommend?
     
  5. Jul 31, 2007 #4

    -Job-

    User Avatar
    Science Advisor

  6. Jul 11, 2010 #5
  7. Jul 25, 2012 #6
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Learning c#, .NET programming
  1. C program (Replies: 0)

  2. C programming (Replies: 9)

Loading...