Day Dreaming

"A daydream is a visionary fantasy experienced while awake, especially one of happy, pleasant thoughts, hopes or ambitions."[Ref:Wikipedia]

Day 6,7 - Chapter 13: C# Language Features


This chapter deals with various other C# features and a relatively simple chapter. I will post the code for this chapter and for previous one together in the next post. Sorry but have been procrastinating little.
Implicity Typed Variables - C# 2008 includes implicitly typed local variable, now provides a new keyword  var, which you can use in place of specifying a formal data type and it will be assigned a type based on its initialization. It is illegal to use the var keyword to define return values, parameters, or field data of a type. Be very aware that implicit typing of local variables results in strongly typed data.

Automatic Properties - In C# we have automatic properties. Automatic property syntax -
public string PetName { get; set; }. We can also restrict access to automatic properties using protected and private properties. Also when we use automatic properties the variables are set to default values e.g. int to 0 and ref variable to NULL.

Extension Methods - Extension methods allow existing compiled types as well as types currently being compiled to gain new functionality without needing to directly update the type being extended. There are two conditions to be followed firstly the methods must be declared within a static class and methods must have this modifier.

Partial Methods  - Partial methods allows you to prototype a method in one file, yet implement it in another file. Restrictions on Partial methods –
• Partial methods can only be defined within a partial class.
• Partial methods must return void.
• Partial methods can be static or instance level.
• Partial methods can have arguments (including parameters modified by this, ref, or params—but      not with the out modifier).
• Partial methods are always implicitly private.

Object Initializer Syntax -  Using this technique, it is possible to create a new type variable and assign a slew of properties and/or public fields in a few lines of code. e.g.
 var yetAnotherPoint = new Point() { X = 30, Y = 30 }; - Default Constructor
var yetAnotherPoint = new Point(12,34) { X = 30, Y = 30 }; -Custom Constructor
We can also initialize collections in similar way.

Anonymous types - When you define an anonymous type, you do so by making use of the new var keyword in conjunction with the object initialization syntax. All anonymous types are automatically derived from System.Object E.g. –
var myCar = new { Color = "Bright Pink", Make = "Saab", CurrentSpeed = 55 };
Anonymous types have following iterations –
• You don’t control the name of the anonymous type.
• Anonymous types always extend System.Object.
• The fields and properties of an anonymous type are always read-only.
• Anonymous types cannot support events, custom methods, custom operators, or custom
overrides.
• Anonymous types are always implicitly sealed.
• Anonymous types are always created using the default constructor.

Also, Since they derive from object they can have the methods of Object Class.Compiler-generated Equals() method makes use of value-based semantics when testing for equality (e.g., checking the value of each field for the objects being compared).

0 comments: