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

In summary, c# is better than c++ because c# has more control over garbage collection, more control over attributes, and delegates.
  • #1
Gza
449
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
  • #2
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:
  • #3
This was an excellent intro, thank you very much. Any good books/resources you recommend?
 
  • #4
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:
  • #5
try this tutorial

http://chsarp.net-informations.com

thank
chan.
 
Last edited by a moderator:

1. What is C# and .NET programming?

C# is a modern, object-oriented programming language developed by Microsoft. It is commonly used for developing desktop applications, web applications, and games. .NET is a software framework also developed by Microsoft that provides a platform for developing and running applications written in various programming languages, including C#.

2. Why should I learn C# and .NET programming?

C# and .NET programming are widely used in the industry, making it a valuable skill for job opportunities. The language is also relatively easy to learn and has a large community of developers, making it a great choice for beginners. It also offers great support and robust tools for building applications.

3. How do I get started with learning C# and .NET programming?

The first step is to download and install the necessary software, which includes the .NET Framework and an Integrated Development Environment (IDE) such as Visual Studio. Then, you can start by learning the fundamentals of C# and gradually move on to more advanced topics. There are many online resources, tutorials, and courses available to help you get started.

4. What are the key features of C# and .NET programming?

C# is a powerful and versatile language, with features such as strong type safety, automatic memory management, and support for modern programming concepts like generics and LINQ. .NET offers a rich set of libraries and tools, including a user-friendly graphical user interface (GUI) for building applications, web development frameworks, and database connectivity.

5. How can I become proficient in C# and .NET programming?

Becoming proficient in C# and .NET programming requires dedication, practice, and continuous learning. It is crucial to have a strong understanding of the language syntax, data types, control structures, and object-oriented principles. It is also beneficial to work on projects and collaborate with other developers to gain hands-on experience and explore different aspects of the language and framework.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
6K
  • STEM Career Guidance
Replies
10
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
22K
  • Programming and Computer Science
Replies
15
Views
5K
Back
Top