Ch.01:B – Close-up with .NET
.NET supports object-oriented programming, with single implementation inheritance of classes and includes encapsulation, inheritance, and polymorphism.
- Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
- Inheritance describes the ability to create new classes based on an existing class.
- Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.
In addition to the above, it also brings in the idea of interfaces which provides a contract, forcing all the classes that implement a given interface to abide by the contract. The classes must implement the methods and properties that are specified by the interface.
I am not going to get into more detail with this topic, since it has a complete chapter dedicated for this purpose. We shall look at OOPs while working with C#.
Strong Data Typing
Arguably Strong Data typing is one of the best features of .NET. Unlike languages like Visual Basic 6 or Scripting languages which uses Variant data type, .NET specifies that all variables are clearly marked as being of a particular data type. It does not support the code that result in ambiguous data types. Though some languages compatible with .NET, such as Visual Basic 2010, still allow some loosely typed data types, when that code is compiled, the compilers behind the scenes ensure that the type safety is enforced in the resulting Intermediate Language code. Also Managed C++ allows pointers to be used, but then the resulting code cannot get the benefits like Garbage collection, etc.
Strong Typing vs. Loose Typing
A type system is said to feature strong typing when it specifies one or more restrictions on how operations involving values of different data types can be intermixed. .NET which supports strong typing prevents the successful execution of an operation on arguments that have the different types.
The opposite of strong typing is loose or weak typing. Weak typing means that a language implicitly converts or casts data types when used.
Consider an example using the Weak typing using the Variant data type.
var x = 5; // x is an integer
var y = "10"; // y is a string
x + y; // Answer differs between Strong Typed Language and Weak Typed Language
In a Strong typed language, when we try to build the above code, it throws an error and we can find out the error and fix it appropriately.
Looking at the above scenario, we can say that using Loose Typing will enable a user to write code faster and makes it easier too, but it also results in some results that we can’t easily predict and may also result in getting errors at runtime. In a Strong typed language, though it might require us to write a little bit more code, we can be sure of the result and any type errors are caught while building rather than at runtime. I would prefer to go with strong typed and write more code to get proper results and sort out any errors at build time rather than at runtime.
Also, in terms of performance, a loosely typed language needs to do more work trying to figure out how a datatype should be interpreted while strong typed language this interpretation is not required, leading to improved performance.
In .NET which supports strong typing, the compiler catches all wrong data type errors and it would take a programmer just few clicks to find and fix the errors. Using Visual Studio, we will get to know that there is a problem, the type of problem, where it is and a click will take you there so that you can fix it. Compare that to loosely typed programming compiler, where you will have to Run the application, when your application throws an error, get back to the code, make the change, compile & execute and continue with the process again.
Type Safety in C#
C# is primarily a strongly typed or type-safe language, where in data types can interact only through protocols they define, thereby ensuring type safety. For example, in C# you cannot interact with a string type as an integer type without type casting the same. In addition to dynamic type safety enforced by .NET CLR at runtime, C# also supports static typing, where in the language enforces type safety at compile time. Static typing eliminates a large class of errors before a program is even run and there by taking away the burden from unit testing onto the compiler by verifying that all the types in a program fit together correctly. Static typing makes large programs a lot easier to manage, more predictable and more robust. In Visual Studio, it allows tools such as IntelliSense to help you write a program easily, since it knows before hand a given variable’s type and hence the methods are provided and properties it provides.
Although, C# 4.0 allows parts of your code to be dynamically typed via the new dynamic keyword, still C# remains a predominately statically typed language.