Anda di halaman 1dari 14

CSharp Difference FAQs-1 1.Difference between C# 3.5 and C# 4.0 S.No 1 C# 3.5 C# 3.

5 does not support dynamic programming. The dynamic keyword is not recognized in C# 3.5. C# 4.0 C# 4.0 supports dynamic programming through dynamic objects. In C# 4.0, dynamic keyword is associated with objects to represent them as dynamic objects. C# 4.0 allows creation of dynamic variables.

3 4

Dynamic variables cannot be created in C# 3.5.

In C# 3.5, method parameters In C# 4.0, the method parameters cannot be specified with can be specified with default values default values. C# 3.5 does not using optional parameters. support optional parameters. In C# 3.5, method parameters have to be specified in the same order as in method declaration in the method call. C# 3.5 does not provide named parameters. In C# 3.5, usage of ref keyword is mandatory while executing methods which are supplied by the COM interfaces. The COM objects indexed properties are not recognized in C# 3.5. Co-variance and contravariance are not supported in Generics of C# 3.5. C# 4.0 provides named parameters to represent the values of method parameters without following the order of declaration.

In C# 4.0, usage of ref keyword is optional while executing methods which are supplied by the COM interfaces. The COM objects indexed properties are recognized in C# 4.0. C# 4.0 enhances Generics by introducing co-variance and contravariance.

2.Difference between C# and VB.NET S.No 1 C# In C#, variables are declared using declarators. VB.NET In VB.NET, the variables are declared using keywords such as private, protected, friend, public, static, shared and Dim.

In C#, an object can only be created using new. In C#, void is specified as the return type when a method does not return a value. In C#, no keyword is required to indicate an overloaded method. The current object is referred using this pointer in C#. Non virtual call cannot be made in C#. Compound data type is declared in C# using class, struct and interface. In C#, constructors are used to initialize an object. Object cleanup can be explicitly done by destructor in C#. In C#, an object is subject to asynchronous modification using volatile keyword.

In VB.NET, an object can be created using new and CreateObject(). In VB.NET, Sub is used in method declaration to indicate that the method does not return any value. In VB.NET, Overloads keyword is used to indicate an overloaded method. The current object is referred as me in VB.NET. To make a non-virtual call to the current objects virtual method, MyClass is used in VB.NET. Compound data type is declared in VB.NET using Structure. In VB.NET, Sub New() is used to initialize the newly created object. Object cleanup can be done by using Finalize method in VB.NET. In VB.NET, an object cannot be modified in an asynchronous way.

5 6

8 9

10

11

In C#, all the variables have to In VB.NET, variables can be forced be declared before being used. for explicit declaration using Option Explicit. In C#, default property is defined by using indexers. In C#, base class can be referred using the keyword base. Abstract class of C# can only be inherited and not instantiated. Sealed keyword of C# is used to denote that a class cannot be inherited. In VB.NET, default property is defined using Default. In VB.NET, base class can be referred using the keyword MyBase. MustInherit keyword of VB.NET is used to denote that a class can only be inherited and not instantiated. NotInheritable keyword of VB.NET denotes that the class cannot involve in inheritance.

12 13

14

15

16 17

Division can be performed in C# using / operator. Remainder of division can be retrieved using mod operator of C#. C# does not have exponentiation operator. C# has Bitwise operators namely &,| and ^. Object reference variables can be compared using == operator in C#. The short ciruit operators in C# are && (Boolean AND) and || (Boolean OR).

Division can be performed in VB.NET using \ operator. Remainder of division can be retrieved using %. In VB.NET, exponentiation can be performed using ^ operator. Bitwise operators in VB.NET are And, Or, Xor. Object reference variables can be compared using Is operator in VB.NET. The short circuit operators in VB.NET are AndAlso (Boolean AND) and OrElse (Boolean OR).

18 19 20

21

3.Difference between C# and C++ S.No 1 C# C# is a high level language that is component oriented. C++ C++ is a low level and indeed platform neutral programming language.

When compiled, C# code is When compiled, C++ code is converted into Intermediate converted into assembly language language code. This code. intermediate language code is converted into executable code through the process called Just-In-Time compilation. In C#, memory management is automatically handled by garbage collector. In C# Switch Statement, the test variable can be a string. In C# switch statement, when break statement is not given, the fall through will not happen to the next case statement if the current case statement has any code. In addition to for, while and do..while, C# has another flow In C++, the memory that is allocated in the heap dynamically has to be explicitly deleted. In C++ Switch Statement, the test variable cannot be a string. In C++ switch statement, when break statement is not given, the fall through will happen to the next case statement even if the current case statement has any code. C++ does not contain for each statement.

4 5

control statement called for each. 7 C# struts can contain only value types. The struts is sealed and it cannot have a default no-argument constructor. In C#, delegates, events and properties can also be specified as class members. C++ struts behave like classes except that the default access is public instead of private.

In C++, only variables, constructors, functions, operator overloads and destructors can be class members. Delegates, events and properties cannot be specified as class members. In C++, the end of the class definition has a closing brace followed by a semicolon.

In C#, the end of the class definition has a closing brace alone.

10

The access modifiers in C# are The access modifiers in C++ are public, private, protected, public, private, protected. C++ internal and protected internal. does not have internal and protected internal access modifiers. C# has finally block in exception handling mechanism. The code statements in the finally block will be executed once irrespective of exception occurrence. The exception in C# can only throw a class that is derived from the System.Exception class. C# does not have the concept of function pointers. C# has a similar concept called Delegates. C++ does not have finally block in exception handling mechanism.

11

12

The exception in C++ can throw any class.

13

C++ has the concept of function pointers.

Reference: http://onlydifferencefaqs.blogspot.in/2012/08/csharp-difference-faqs-1.html

Object vs Var vs Dynamic Keywords Difference between Object, Var and Dynamic Keywords Object Introduction: Object keyword is introduced in C# 1.0 Data Storage: Can able to store any kind of value, because object is the base class of all type in .net framework. Var Dynamic is

Introduction: Introduction: Var keyword is introduced Dynamic keyword in C# 3.0 introduced in C# 4.0 Data Storage: Can able to store any type of value but it require to initialize at the time of declaration. http://2.bp.blogspot.com/f4R_nVJyN7Q/TjKtHKB0 NNI/AAAAAAAACcE/vZm riKOlC5Y/s1600/var+iniali ze+error.JPG Type Safety: It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time. http://4.bp.blogspot.com/BXXEgzeJ8XE/TjKtWeI_J GI/AAAAAAAACcQ/iW6x h5sYPg4/s1600/var+com piler.JPG Supports as method argument type : Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined. http://4.bp.blogspot.com/dkV7VvttM34/TjKtbFDf0yI /AAAAAAAACcU/PnUFg5 KIDcE/s1600/var+passing +argument.JPG

Data Storage: Can able to store any type of the variable, similar to old VB language variable.

Type Safety: Compiler has little information about the type . http://1.bp.blogspot.com /O3j4Ud1rn9s/TjKtOoGs rbI/AAAAAAAACcI/Q1a 9HF2Hbok/s1600/object .JPG

Type Safety: Compiler doesn't have any information about the this type of variable. http://4.bp.blogspot.com/9B9snB1pPFg/TjKtRr435OI /AAAAAAAACcM/FnUjqaG FYI0/s1600/dynamic.JPG

Supports as method argument type : Object type can be passed as function argument and function also can return object type

Supports as method argument type : Dynamic type can be passed as function argument and function also can return object type

Need type casting on assignment :

Need type casting on Need type casting assignment : assignment :

on

Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation. Allows to perform operation of given type once it get cast any user defined or primitive data type. Stored Value: Cause the problem at run time if the stored value is not get converted to underlying data type. http://2.bp.blogspot.com /AcTarSXqc08/TjKtgMQjiI/AAAAAAAACcY/ldRU TGHpTO4/s1600/Objec t+error.JPG As source in for each iteration: Not supported Application Area: Useful when doesn't have more information about the data type i.e., to hold objects as base class. Compilation Example: Object o = new Button(); o.Width doesnt compile

No need to cast because Casting is not require but compiler has all you need to know the information to perform property and methods operation. related to stored type

Stored Value: Doesn't cause problem because compiler has all info about stored value.

Stored Value: Cause problem if the wrong method or property accessed because all the information about stored value is get resolved only at run time http://2.bp.blogspot.com/Gk8fm_tIc98/TjKtjhCgnPI/A AAAAAAACcc/BZshBUrwC 90/s1600/dynamic+error.JP G

As source in for each As source in for each iteration: iteration: Supported Supported Application Area: Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature. Application Area: Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code. Compilation Example: dynamic o = new Button(); o.Width and even o.xyz() will compile where xyz is not a member

Compilation Example: var o = new Button(); o.Width will compile

Reference: http://onlydifferencefaqs.blogspot.in/2012/09/difference-between-object-varand.html

Identifier vs Keyword in C# Difference between Identifier and Keyword in C# S.No 1 Identifier Meaning: A C# identifier is a name given to a class, delegate, interface, method, namespace or variable. What it consists of ? Must be any combination of letters, numbers, and underscores. Upper case / Lower case / Both ? Allows both Uppercase and Lowercase. Whether Unicode characters are allowed or not ? Identifiers can begin with an allowed Unicode character or an underline. But,we need to use nonformatting Unicode characters in any part of an identifier. i.e., u0027format // error Unicode formatting character which is not allowed. Whether it can be used to name classes, methods, or variables ? The purpose of Identifier is to provide names to namespaces, classes, methods, variables, and interfaces. Keyword Meaning: Keywords are predefined reserved identifiers that have special meanings to the compiler. What it consists of ? Must be only letters.

Upper case / Lower case / Both ? Most of the keywords are Lowercase.But,only Namespaces, Types and Members have their first character in uppercase. Whether Unicode characters are allowed or not ? Unicode characters are not used in any C# keywords

Whether it can be used to name classes, methods, or variables ? We cannot use keywords for naming classes, methods, or variables.

Note: Keywords cannot be used as identifiers in your program unless they include the "at" sign (@) as a prefix. For example, @new would be a legal identifier; but, new is not, because it is a keyword. Reference: http://onlydifferencefaqs.blogspot.in/2012/09/identifier-vs-keyword-in-c.html

null keyword vs DBNull class in C# Difference between null and System.DBNull.Value S.No 1 null Conceptual Difference: The keyword null represents an invalid reference. What is the purpose of null keyword ? NULL is used to check if a whole record is not found in the database. Is it a Value / Reference / Nullable type ? null is the default value of reference-type variables. Ordinary value types cannot be null. DBNull Conceptual Difference: The class System.DbNull represents a nonexistent value in a database field. What is the purpose of DBNull class ? DBNULL is used to check if a certain field in a database is null. Is it a Value / Reference / Nullable type ? DBNull does not match the type of a nullable value type, a reference type, or a value type, and therefore is not directly assignable. Please look at: Handle null, then DBNull to avoid InvalidCastException Reason: Of DBNull class only one instance can exist as it a singleton class. Sole instance is DBNull.Value. Handle null, then DBNull to avoid InvalidCastException: For instance, sometimes in code you will see items in a data row being used without checking for null first. When the underlying field is not null, this is OK, but if a null is available, the following code will not work: Example-1 Decimal amount = (Decimal)row["AmountValue"]; //returned value is DBNull Because DBNull is not the same type, an InvalidCastException occurs. Rather, a null check has to be performed. The safe alternative to this is the following: Example-2 Decimal amount = 0; If (!row.IsNull("AmountValue")) amount = (Decimal)row["AmountValue"];

If you want to set a row's value, you can do something like below. The row takes an instance of type object, and therefore can be assigned DBNull: Example-3 If (amountValue > 0) row["AmountValue"] = amountValue; Else row["AmountValue"] = DBNull.Value; The value property returns the actual instance of DBNull (a static property). Sometimes, a helper routine would be good to handle this conversion for you, reducing the amount to a total of one line instead of four. That may not seem like much, but a table with thirty columns will make this much coding a chore. Checking null and DBNull values How to check for null value ? 1. Create the variable. A variable must be declared before using it. The code below allocates memory and declares the variable for use. string myNullVar = null; The code uses the "null" value assignment for the new variable. 2. Check if the variable is null. The code below checks if the variable is null. If it is null, the code assigns it a color value. if(myNullVar == null) { myNullVar = "purple"; } 3.Check if the variable is not null. In some cases, you may wan to reassign the variable back to null. The following code checks if the variable is not null, and assigns it a null value. if(myNullVar != null) { myNullVar = null; } How to check for DBNull values ? 4. Declare a variable. Just like section 1, to check for DBNull, a variable needs to be declared. However, the variable is assigned the DBNull value. string myNullVar = System.DBNull; 5. Use the "Equals()" method from the string class to check for the DBNull value. The code below checks for this value and assigns myNullVar a color. if (myNullVar.Equals(System.DBNull.Value)) { myNullVar = "purple"; } 6.Evaluate if the variable is not null. Similar to section one, you may want to check if the variable is not null to reassign it back to DBNull. The "!" character evaluates to "not" in the following example. if(!myNullValue.Equals(System.DBNull.Value)) { myNullValue = System.DBNull;

} Note: However note that neither checking null of nor DBNull.value will work if the index is out of range. If we check for an index or a column that doesn't exist, it will throw an index out of range exception. Reference: http://onlydifferencefaqs.blogspot.in/2012/09/null-keyword-vs-dbnull-class-inc.html C# generics vs C++ templates Difference between C# generics and C++ templates S.No 1 C# Generics Strongly / Loosely typed: C# Generic types are strong typed. Instantiated at Compile time / Runtime: C# Generic types are instantiated at the runtime. Whether it permits the type parameters to have default values ? C# Generic types do not permit the type parameters to have default values. Whether DLL can be created using C# Generics ? Libraries or DLLs can be created using C# Generics. C++ Templates Strongly / Loosely typed: C++ Templates are loosely typed. Instantiated at Compile time / Runtime: C++ templates are instantiated at the compile time. Whether it permits the type parameters to have default values ? C++ templates permit the type parameters to have default values. Whether DLL can be created using C++ Templates ? C++ templates are always expanded at compile time. Therefore, the C++ compiler must have access to all template types generally through header files and any types used to create the closed types from the template types at compile time. For this reason alone, it is impossible to package C++ templates into libraries or DLLs. The STL is a perfect example: notice that almost every bit of your favorite STL implementation exists in header files.

Whether it is possible to call arithmetic operators in a C# generic class ? C# generics do not provide the same amount of flexibility as C++ templates. For example, it is not possible to call arithmetic operators in a C# generic class, although it is possible to call user defined operators. Whether explicit specialization is supported by C# generics ? C# generics does not support explicit specialization; that is, a custom implementation of a template for a specific type. Whether partial specialization is supported by C# generics ? C# generics does not support partial specialization: a custom implementation for a subset of the type arguments. Whether type parameter can be used as the base class for the generic type ? C# does not allow the type parameter to be used as the base class for the generic type. Whether generic type parameter can itself be a generic ? In C#, a generic type parameter cannot itself be a generic, although constructed types can be used as generics. What type of code it allows ? C# requires code in a class to be written in such a way that it will work with any type that satisfies the constraints.C# disallows as allowed in C++ (Please look at example from R.H.S column); the only language constructs allowed are those that can be deduced

Whether it is possible to call arithmetic operators in a C++ template class ? With C++ template class, it is possible to call arithmetic operators.

Whether explicit specialization is supported by C++ templates ? C++ template support explicit specialization.

Whether partial specialization is supported by C++ templates ? C++ template support partial specialization.

Whether type parameter can be used as the base class for the template type ? C++ allows the type parameter to be used as the base class for the template type. Whether template type parameter can itself be a template? C++ does allow template parameters.

10

What type of code it allows ? C++ allows code that might not be valid for all type parameters in the template, which is then checked for the specific type used as the type parameter. For example, in C++ it is possible to write a function that uses the arithmetic operators + and - on objects of the type parameter,

from the constraints.

which will produce an error at the time of instantiation of the template with a type that does not support these operators. Which language supports templates ? Templates are a C++ only language feature. Whether it allows non-template parameters ? C++ allows non-type template parameters.

11

Which language supports generics? Generics can be supported by any .NET language that wishes to do so. Whether it allows nontemplate parameters ? C# does not allow non-type template parameters.

12

Reference: http://onlydifferencefaqs.blogspot.in/2012/09/c-generics-vs-c-templates.html is vs as operators in C# Difference between is and as operators in C# S.No 1 Is operator Meaning: The is operator allows us to check whether an object is compatible with a specific type. Here the phrase "is compatible" means that an object is either of that type or is derived from that type. As opeator Meaning: The as operator is used to perform certain types of conversions between compatible reference types. i.e., The as operator is used to perform explicit type conversion of reference type.If the type being converted is compatible with the specified type,then that conversion is successful else as operator will return a null value. How it works ? i.Checks whether Object is compatible with given type ii. If yes, returns not null reference i.e same object iii. If no, returns null instead of raising an exception.

How it works ? i.Checks whether Object is compatible with given type ii. If yes, returns true iii. If no, returns false Remarks: An is expression evaluates to true if both of the following conditions are met: expression is not null. expression can be cast to type. That is, a cast expression of the form (type)(expression) will

complete without throwing an exception. For more information, see 7.6.6 Cast expressions. 3 When to use is operator ? The is-cast is only ideal if the casted variable will not be needed for further use. For what type of conversions,is operator can be used ? is operator only considers reference conversions, boxing conversions, and unboxing conversions. Syntax: expression is type Example: int k = 0; if (k is object) { Console.WriteLine("i is an object"); } When to use as operator ? If we need to use the casted variable, use the as-cast. For what type of conversions,as operator can be used ? as operator only performs reference conversions and boxing conversions.

5 6

Syntax: expression as type Example: object x = "I am here"; object y = 10; string s1 = x as string; // here conversion is compatible so,s1="I am Here" string s2 = y as string;// here conversion is imcompatible so,s2=null

Notes: 1.Syntax: expression as type is equivalent to: expression is type ? (type)expression : (type)null Example: Employee e = obj as Employee; is equivalent to: Employee e = obj is Employee ? (Employee)obj : (Employee)null; 2.Both is and as operators cannot be overloaded. 3.The is operator will never throw an exception. 4. A compile-time warning will be issued if the expression expression is type is known to always be true or always be false. 5. Both is and as operators cannot use user-defined conversions 6.The as cast is an efficient, elegant way to cast in the C# language. It promotes exception-neutral and less redundant code.

Reference: http://onlydifferencefaqs.blogspot.in/2012/09/is-vs-as-operators-in-c.html

Anda mungkin juga menyukai