Florence Blogspot about asp.net tutorials and web design and web development

Tuesday, January 22, 2008

C# Frequently asked questions

FAQ Questions
What is C#?
Does C# replace Java?
Does C# replace C++?
What does a simple C# program look like?
Is C# object-oriented?
Does C# have its own class library?
How do I develop C# apps?
Are there any free C# IDEs that I could use?
Can I program 3D games with C#?
What is Common Language Runtime (CLR)?
What is boxing and unboxing?
Are there pointers in C#?
What are delegates?
Are there Certification exams for C#?
Can I use MySQL with C#?
Can I use DirectX in C#?
Is it possible to have a static indexer in C#?
Can I use inline assembly or IL in C# code?
Does C# have macros or a preprocessor?
Does C# have templates like C++?
Why can't I use static and const together?

FAQ Answers

Q: What is C#?

A: C# is a programming language designed by Microsoft. It is loosely based on C/C++, and bears a striking similarity to Java in many ways. Microsoft describe C# as follows:"C# is a simple, modern, object oriented, and type-safe programming language derived from C and C++. C# (pronounced 'C sharp') is firmly planted in the C and C++ family tree of languages, and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of Visual Basic and the raw power of C++."

Q: Does C# replace Java?
A: C# is a very Java-like language - the core of both languages have similar advantages and limitations when compared to C++. For example, both languages have garbage collection, but neither language has templates. Microsoft have ceased production of Visual J++, so it's hard not to view C# as Microsoft's alternative to Java.

Q: Does C# replace C++?
A: The obvious answer is no. However it's difficult to see C++ as the best choice for new .NET code. For the .NET runtime to function fully, it requires the programming language to conform to certain rules - one of these rules is that language types must conform to the Common Type System (CTS). Unfortunately many C++ features are not supported by the CTS - for example multiple inheritance of classes and templates.Microsoft's answer to this problem is to offer Managed Extensions (ME) for C++, which allows you to write C++ that conforms to the CTS. New keywords are provided to mark your C++ classes with CTS attributes (e.g. __gc for garbage collection). However, it's difficult to see why ME C++ would be chosen over C# for new projects. In terms of features they are very similar, but unlike C++, C# has been designed from the ground-up to work seamlessly with the .NET environment. The raison d'etre for ME C++ would therefore appear to be porting existing C++ code to the .NET environment.So, in answer to the question, my suspicion is that C++ will remain an important language outside of the .NET environment, and will be used (via ME) to port existing code to .NET, but I think C# will become the language of choice for one-time C++ developers developing new .NET applications. But only time will tell ...


Q: What does a simple C# program look like?
A: This is the Hello World program:
class CApplication
{
public static void Main()
{
System.Console.Write( "Hello, new .NET world." );
}
}

Q: Is C# object-oriented?
A: Yes, C# is an OO language in the tradition of Java and C++.

Q: Does C# have its own class library?
A: Not exactly. In common with all .NET languages (e.g. VisualBasic?.NET, JScript.NET) C# has access to the .NET class library. C# does not have its own class library.

Q: How do I develop C# apps?
A: The (free) .NET SDK contains the C# command-line compiler (csc.exe). Visual Studio.NET has fully integrated support for C# development. You need to download the .NET SDK and then whatever compiler or IDE you'd like to use.Q: Are there any free C# IDEs that I could use?

Q: What is Common Language Runtime (CLR)?
A: As part of Microsoft's .NET Framework, the Common Language Runtime (CLR) is programming that manages the execution of programs written in any of several supported languages, allowing them to share common object-oriented classes written in any of the languages. The Common Language Runtime is somewhat comparable to the Java virtual machine that Sun Microsystems furnishes for running programs compiled from the Java language. Microsoft refers to its Common Language Runtime as a "managed execution environment." A program compiled for the CLR does not need a language-specific execution environment and can easily be moved to and run on any system with Windows 2000 or Windows XP.Programmers writing in any of Visual Basic, Visual C++, or C# compile their programs into an intermediate form of code called Common Intermediate Language (CIL) in a portable execution (PE) file that can then be managed and executed by the Common Language Runtime. The programmer and the environment specify descriptive information about the program when it is compiled and the information is stored with the compiled program as metadata. Metadata, stored in the compiled program, tells the CLR what language was used, its version, and what class libraries will be needed by the program. The Common Language Runtime allows an instance of a class written in one language to call a method of a class written in another language. It also provides garbage collecting (returning unneeded memory to the computer), exception handling, and debugging services.

Q: What is boxing and unboxing?
A: Boxing and unboxing is a essential concept in C#’s type system. With Boxing and unboxing one can link between value-types and reference-types by allowing any value of a value-type to be converted to and from type object. Boxing and unboxing enables a unified view of the type system wherein a value of any type can ultimately be treated as an object. Converting a value type to reference type is called Boxing. Unboxing is an explicit operation.

Q: Are there pointers in C#?
A: There are no pointers in the C# language, although you can use pointers in your C# applications, as long as you put them into the "unsafe code" space:…
unsafe
{

// unsafe context: can use pointers here

}


Q: What are delegates?
A: A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.


Q: Are there Certification exams for C#?
A: Yes, in fact, there already are 3 exams, 70-315, 70-316, and 70-320. Check these links for more information.http://www.microsoft.com/traincert/exams/70-315.asphttp://www.microsoft.com/traincert/exams/70-316.asphttp://www.microsoft.com/traincert/exams/70-320.asp

Q: Can I use MySQL with C#?
A: Yes, you can use MySQL with C#.You can connect to the database by using ODBC driver and the System.Data.Odbc namespace.

Q: Is it possible to have a static indexer in C#?
A: No, they are not allowed in C#.

Q: Can I use inline assembly or IL in C# code?
A: No.

Q: Does C# have macros or a preprocessor?
A: C# doesn't have macros as such, nor does it strictly speaking have a pre-processor, but it does have conditional compilation symbols which can be used to affect compilation. These can be defined within code or with parameters to the compiler. The "pre-processing" directives in C# (named solely for consistency with C/C++, despite there being no separate pre-processing step) are (text taken from the ECMA specification):#define and #undefUsed to define and undefine conditional compilation symbols#if, #elif, #else and #endif Used to conditionally skip sections of source code#line Used to control line numbers emitted for errors and warnings.#error and #warning Used to issue errors and warnings.#region and #endregion Used to explicitly mark sections of source code.See section 9.5 of the ECMA specification for more information on the above. Conditional compilation can also be achieved using the Conditional attribute on a method, so that calls to the method will only be compiled when the appropriate symbol is defined. See section 24.4.2 of the ECMA specifcation for more information on this.

Q: Does C# have templates like C++?
A: Although C# doesn't have templates, and isn't likely to get them, it is getting a feature called generics which will be available in Whidbey, the next version of .NET, which should be available in the first half of 2005. (A beta version is currently available.) Generics will be a feature in the CLR itself, and most languages targetting the CLR will change to support it. Generics provide a lot of the functionality of C++ templates (mostly in terms of type safety) but in a more restricted (and therefore potentially less confusing) way.

Q: Why can't I use static and const together?
A: All constants declarations are implicitly static, and the C# specification states that the (redundant) inclusion of the static modifier is prohibited. I believe this is to avoid the confusion which could occur if a reader were to see two constants, one declared static and one not - they could easily assume that the difference in specification implied a difference in semantics. (Having said that, there is no prohibition on redundantly specifying an access modifier which is also the default one, where there is a choice. For instance, a (concrete) method can be explicitly marked as private despite that being the default. The rule appears to be that where there is no choice (e.g. a method declaration in an interface) the redundant modifier is prohibited. Where there is a choice, it's allowed.

No comments:

Search