The following code won’t work, because conn goes out of scope before you enter the catch block.
[code:c#]
The following code won’t work, because conn goes out of scope before you enter the catch block.
[code:c#]
Microsoft provides a very full language equivalents page which compares not only C# and VB.NET, but also other languages targeted at the .NET framework. It looks at the equivalent concepts, keywords, types, operators etc. A very valuable resource when you’re trying to read or write code in a language which isn’t your preferred one.
No, C# doesn’t have an equivalent of VB.NET’s MyClass keyword. If you want to guarantee not to call an overridden version of a method, you need to make it non-virtual in the first place.
To create delegate instances in C#, you just specify the delegate type, the method, and (if you want to create a delegate targetting a different instance or type from the current one) the target. For instance, each of these creates a ThreadStart delegate:
ThreadStart x1 = new ThreadStart(SomeInstanceMethod); ThreadStart x2 = new ThreadStart(AnotherType.SomeStaticMethod); ThreadStart x3 = new ThreadStart(someVariable.SomeInstanceMethod);
Use String.Substring. Assuming that x is a string of length at least n, to get the last n characters, you would use x.Substring(x.Length-n).
Note that the above assumes that the string is at least n characters long. For a more robust version, you might use something like: x.Length < n ? x.Substring(x.Length-n) : x.
Me in C# is this, and MyBase in C# is base. To access normal members, just use this.memberName or base.memberName. For information about chaining constructors together, see my article on constructors.
For reference types, the equivalent of VB’s Nothing is C#’s null. For value types, it’s the default value – 0, false, etc.
Many people, including the C# language designers, believe that ‘with’ often harms readability, and is more of a curse than a blessing. It is clearer to declare a local variable with a meaningful name, and use that variable to perform multiple operations on a single object, than it is to have a block with a sort of implicit context.
For more information, see the Ask a C# Language Designer page.
The choice between C# and VB.NET is largely one of subjective preference. Some people like C#’s terse syntax, others like VB.NET’s natural language, case-insensitive approach. Both have access to the same framework libraries. Both will perform largely equivalently (with a few small differences which are unlikely to affect most people, assuming VB.NET is used with Option Strict on). Learning the .NET framework itself is a much bigger issue than learning either of the languages, and it’s perfectly possible to become fluent in both – so don’t worry too much about which to plump for. There are, however, a few actual differences which may affect your decision:
Option Strict off – type safety at compile time goes out of the window, but legacy libraries which don’t have strongly typed interfaces become easier to use.Microsoft.VisualBasic namespace, and can be used by other languages with a reference to the Microsoft.VisualBasic.dll). Many of these can be harmful to performance if used unwisely, however, and many people believe they should be avoided for the most part.with construct: it’s a matter of debate as to whether this is an advantage or not, but it’s certainly a difference.Catch ... When ... clauses, which allow exceptions to be filtered based on runtime expressions rather than just by type.using statement, which makes unmanaged resource disposal simple.Despite the fact that the above list appears to favour VB.NET (if you don’t mind waiting for Whidbey), many people prefer C#’s terse syntax enough to make them use C# instead.