What is the difference between strong typing and weak typing in .NET?

Strong typing means that the type check is done at compile time and weak typing means that the type check is done at run time. .NET languages incorporate strong typing.



Every variable in .NET should have a type associated and the value assigned to the variable should match its type. If these rules are violated then compile time error will be displayed. This avoids abrupt error messages to be displayed to the User at run time. Consider the following example:

public class sampleClass {
public static void Main() {
sampleNo = 10;
}
}

In this example, the variable sampleNo is not declared with any type. This will be detected by the compiler during compilation and you will get the following error:

“The name ‘sampleNo’ does not exist in the current context”

Also, the value assigned to the variable should be acceptable by the type. Consider the following example:
public class sampleClass {
public static void Main() {
int sampleNo=10;
string sampleStr = 100;
}
}

In this example, note that the statement “int sampleNo = 10” is legal because you create a variable sampleNo, declare its type to Integer and define its value to be 10, an integer value. But the next statement “string sampleStr=100” is incorrect because you create a variable called sampleStr, declare its type to String and define its value to be 100, an integer value instead of string value. Hence during compilation, you will get the following error:
“Cannot implicitly convert ‘int’ to ‘string’”

However there are situations when you cannot associate the actual type to a variable at compile time itself. In such cases, you can declare the variable with type var in C# version 3.0 or higher. Such variables can hold values of any type and the actual type will be associated at run time.

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.