Hi..
Ques : What is strong-typing versus weak-typing in ASP.NET? Which is preferred? Why?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Mar 20, 2012, 12:11 AM
Sam HobbsPosted Mar 19, 2012, 11:55 PM
SenthilkumarPosted Mar 19, 2012, 11:43 PM
For example
int i =100;
It is integer type and type specific.
string str = "senthil" //It is type safe and strongly typed
int i = 65;
string str = "kumar";
object obj = i;
object obj = str;
int k = (int) obj;
Now you know what happens when you convert the type.
It will give the runtime error and weakly typed.
Preferred:
It always prefers to use the strongly typed and you can use the generic template to store type specific.
Because it does not need any type casting and which will avoid the run time error.
It will decrease the performance of the application.
Sam HobbsPosted Mar 19, 2012, 9:04 PM
ASP.Net is not considered a language. The languages usually used in ASP.Net are C# and VB. You probably meant to ask about strong typing and weak typing in C# and VB and Vulpes tried to answer that question.
Note that strong typing is generally considered better by enthusiasts of langauges that support strong typing and weak typing is considered better by enthusiasts of languages that support weak typing. I certainly consider strong typing to be better. I even do not like implicit types (the var keyword).
In Vulpes's code, "s" will always be a string and "i" will always be an int. Therefore they are strongly typed. In weak typing, it is possible to assign a string to a variable, then assign an integer to the same variable. Then depending on what type of data that the variable contains, the program might get an exception or it might not. The program cannot assume that a variable is something; a variable can be of any type that the program uses for the variable. So a certain line of code in a program might work many times and many executions but then crash the program when it processes a variable that has been set to a type that the source code line does not expect.
VulpesPosted Mar 19, 2012, 8:10 AM
Weak-typing means that variables can contain or refer to instances of ANY type.
Although a language such as C# is generally strongly-typed, you can do weak-typing using variables of either the object or dynamic types. For example:
string s = "Hello"; // strongly-typed
int i = 3; // strongly-typed
object o1 = s; // weakly-typed
object o2 = i; // weakly-typed
Strong-typing should normally be preferred over weak-typing because it enables more errors to be identified at compile time. Also, there is no need for boxing/unboxing when value types (such as int, double, bool etc) are assigned to object variables which slows down your code.