C# is highlighted as a powerful language, particularly for those transitioning from C++ or Java, due to its modern features and syntax. Key advantages of C# include support for lambda expressions and LINQ in .NET 3.0, which facilitate SQL-like queries on collections. The language allows for more concise property definitions, reducing the need for explicit getters and setters. C# also supports indexed properties, foreach loops, and enhanced garbage collection control. Thread synchronization is simplified with the lock statement, and the use of delegates and events enables extensible programming patterns. Resources for learning C# include various online tutorials, with recommendations for specific sites. Overall, C# is appreciated for its streamlined syntax and robust features, making it an attractive option for developers.
#1
Gza
446
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)?
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
Gza
446
0
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:
#5
chan_lemo
6
0
try this tutorial
http://chsarp.net-informations.com
thank
chan.
Last edited by a moderator:
#6
Xethron
2
0
For others interested, 1st link has changed and 2nd link has a typo...
i am a cse student and as a second year student i started building apps. by sing chatgpt i am getting frontend files and backend files but i fail to connect those files. how to learn it and when i asked my friend he said learn about api keys. should i learn that or learn something new
I've tried to intuit public key encryption but never quite managed.
But this seems to wrap it up in a bow.
This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher.
Is this how PKE works?
No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.