- Provides logical grouping of related types.
- Increases code readability
Saturday, February 23, 2008
Type Spoofing and CLR
Type spoofing is the cause of many security breaches and
compromises an application's stability and robustness. Type safety is therefore an extremely
important part of the CLR.
CLR checks the type security at runtime
Casting Object to a specific type will compile all fine, but at run time an exception 'InvalidCastException' is thrown if the type boxed in Object cannot be converted to the specific type ( i.e is not of the specific type or its base type )
Use As Over Is : using Is and then casting results in type checking twice. Once for Is and then while casting the CLR does the default type check. To avoid this use the As operator.
Where is System.Object ?
System.Object is defined in MSCorLib.dll . This dll contains all the FCL types like Object ,Int32, String,etc.
All the objects in C# are derived from System.Object class.
- There are not data members in this class.
- This class offers 4 public instance methods:
- Equals()
- ToString()
- GetHashCode()
- GetType()
- Two protected methods:
- MemberwiseClone
- Finalize
Thursday, February 21, 2008
Property , C#
In a way exposing the data members in a class as 'public' serves the same purpose as having the same member exposed via Property with simple get/set definitions.However the former is against the concept of Data Encapsulation , which states that the your type's fields should never be public exposed.
Usual Practice: To have a public 'get' and a protected 'set' accessor. Property in this case is listed with least restrictive accessibility ( I.e public in this example)
GetSetMethod Vs Property : is the same thing , just that with the method the code does not look that pretty.Properties offers more simplified syntax.
Backing Field: is the underlying private member access-wrapped in a property.
Code Inlining : At compile time the code for simple get set methods is Inlined by the compiler , in the method the property is accessed.This is only true for Release build and not Debug.
Assign a custom name to a Parameterful Properties ( Indexer) : System.Runtime.CompilerServices.IndexerNamee.g [lndexerName("Update")]
Still this cannot be used in C# to overload Indexers by Name ,we can only overload by parameter type.
System.Reflection.PropertyInfo : used to read the Property at runtime.
Deceptive:
- property may return data that's not part of its state.
- Confusing to tell by looking at the code if the property is full read-only or write only.
Monday, February 18, 2008
Exceptions
Exceptions allow the developers to isolate the failure recovery code from the code required to get the work done.
CLR Compilers
Microsoft provides several compilers that produce the code targeting the runtime: C++/CLI, C# , Visual Basic.NET and JScript.
C++/CLI(Common Language Infrastructure) is Microsoft's language specification to supersede MC++ ( Managed Extensions for C++ )
Microsoft has a version of .NET available for FreeBSD, Windows and Mac OS X called the Shared Source CLI (Rotor).
Technology
Technology has evolved providing abstraction layers to developers to allow them to focus more on the problem by caring less on the micro implementation details. When there are various different technologies offering abstractions over different areas ( web,win, com, etc ) the developers face the challenge when integrating these together to make them talk to each other.
The dot net framework (CLR + FCL) offers all of these abstraction layers on a common platform (IL).
CLR offers an object oriented programming model that defines what types and objects are and how they behave.
FCL - Framework Class Library offers an object oriented API set that all applications use.