Why is c# and .NET programming considered superior to c++/java/VB?

  • Context: C/C++ 
  • Thread starter Thread starter Gza
  • Start date Start date
  • Tags Tags
    Programming
Click For Summary
SUMMARY

C# and .NET programming are considered superior to C++, Java, and VB due to their modern features and syntactic advantages. Key enhancements in .NET 3.0, such as lambda expressions and LINQ, facilitate SQL-like queries on collections. C# offers improved encapsulation through properties, indexed attributes, and simplified syntax for loops and delegates, enhancing code readability and maintainability. The ability to manage garbage collection and utilize thread synchronization with the lock() statement further solidifies C#'s position as a preferred choice for developers transitioning from C++.

PREREQUISITES
  • Understanding of object-oriented programming concepts
  • Familiarity with C++ syntax and programming paradigms
  • Basic knowledge of SQL for database interactions
  • Awareness of threading and synchronization in programming
NEXT STEPS
  • Explore C# properties and indexed attributes for better encapsulation
  • Learn about LINQ and its applications for data manipulation in C#
  • Study the use of delegates and events for creating extensible applications
  • Investigate threading and synchronization techniques in C# using the lock() statement
USEFUL FOR

Software developers, particularly those transitioning from C++ or Java, as well as anyone interested in modern programming practices and enhancing their skills in C# and .NET development.

Gza
Messages
446
Reaction score
0
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)?
 
Technology news on Phys.org
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:
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:
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:
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:
String[] ss = new String[]{ "a", "b", "c" };
for(int i=0; i<ss.length(); i++){
     doSomething(ss[i]);
}

With:
Code:
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:
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:
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:
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:
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:
This was an excellent intro, thank you very much. Any good books/resources you recommend?
 
I use the following site as a C# reference:
http://www.softsteel.co.uk/tutorials/cSharp/lesson1.html

It's the first one i found back when i was learning C#, there might be some other ones. You might also search MSDN for any articles on moving from C++/Java to C#.
 
Last edited by a moderator:
try this tutorial

http://chsarp.net-informations.com

thank
chan.
 
Last edited by a moderator:

Similar threads

Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 13 ·
Replies
13
Views
6K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
7
Views
22K