Friday 12 April 2013

Productivity: Two command-prompt time savers

Recently while watching pluralsight.com videos I noticed that the authors use a console window which allowed them to select and copy text using mouse. And they also had auto-completion for files/directories. As a developer, we work a lot with console applications now and then and having these two features should be a huge time saver.

1> Text selection, copy and paste

Normally we have to click Edit –> Mark –>  Select text –> Press Enter, but there is a quicker way!

- Right-click the title bar and click Properties.

image

image

 

 

 


 

 

 

 


 

- In the Options tab, look for “Quick Edit Mode” and enable it.

image

 

 

 

 

 

 


 

With this setting, we can use left-button mouse to select text and then right-button click or ENTER to copy it. And to paste something, we can click right-button.

image

 

 

 

 


 

 

 




2> Auto-completion for files/directories:

For the auto-completion we need to modify the registry. The steps are:
- Open regedit.exe
- Look for HKEY_CURRENT_USER\Software\Microsoft\Command Processor
- Change the value of CompletionChar to 9.

image

 


 

 

 

 




 

On my computer this was already set to 9, it could be different on yours. Using screen shot I cannot show you how it works, but start by pressing [TAB] on console and it will iterate the directories/files one by one. And when you type in ‘CD D:\[TAB]’ then it would again do the same thing for D:\ drive.

You can also type in a letter, say ‘w’, and then press [TAB] – it will one by one go through all the items in the folder which start with ‘w’.

Hope knowing this saves a few minutes of your life.

Tuesday 9 April 2013

C#: object, var and dynamic

One of the interesting concept introduced in C# 4 was the “dynamic” keyword. Let’s see how it is different from the “var” or our old friend “object”.

1. System.Object has been with us since the birth of .NET framework. It is the base class of almost everything. It means we can write:

1:      object ostring = "Hello";
2:      object oint = 555;
3:      oint = "Hello int"; //point to a string
4:      ostring = -100;//point to an int

Usage: It is used when the type could be anything. For example a method using object parameter to accept any type and then casting it to appropriate type or use reflection to call operations; or a collection which can hold any type. However with the advent of generics ‘object’ is used less often when creating collections. The primary goal of object class is to be a base class and offer low-level services, which include methods which are common to almost every type in .NET framework or created by users. Methods like ToString(), Equals(), GetHashCode(), MemberwiseClone() are provided in the object class which can be reused or overridden (except Finalize of course which can’t be overridden).

2. “var” was introduced in C# 3.0 mainly to support anonymous types created using LINQ.

Taking this LINQ query:

1:  var courses = from course in Courses
2:                where course.Category.Contains("C#")
3:                select new {course.Title, course.Description, course.VideoLength};

The query will return a collection of type IEnumerable. What do you think will be the type of the items in the IEnumerable returned?

If we run this line:


courses.First().GetType().ToString();
//above line returns: <>f__AnonymousType0`3[System.String,System.String,System.String]

we get a type name which is generated dynamically!

But we may ask why not just use ‘object’ type to write it this way:

1:  IEnumerable<object> courses = from course in Courses
2:                where course.Category.Contains("C#")
3:                select new {course.Title, course.Description, course.VideoLength};
4:   
5:  foreach (var course in courses)
6:  {
7:      course.Dump();
8:  }

Yes it compiles without error but you will face a problem when you iterate the collection’s items. Let me attach a screen shot to show what I mean:

image

Since we specified <object> in the IEnumerable we get types as objects. But if we use var – it would implicitly create a new type and expose its properties to you. Again a screen shot with ‘var’

image

That’s the power of anonymous types, you don’t need to declare a new class with the three properties which you want from the collection. Let the compiler do everything for you!

Usage: ‘var’ cannot be used if they are not initialized and they cannot be used in method parameters.

   1:  void Main()
   2:  {
   3:      var i; //Error: Implictly-typed local variables must be initialized
   4:  }
   5:   
   6:  void Test(var i) //Error: The contextual keyword 'var' may only appear within a local variable declaration
   7:  {
   8:      
   9:  }

Also once the var is declared with a specific type it cannot be assigned a value which is not compatible. For e.g.

1:      var i = 123;
2:      i = false; //Error: Cannot implictly convert type'bool' to 'int'

3. “dynamic”: Dynamic was introduced in C# 4.0. It is very different from ‘var’ which is evaluated at compile time, where the dynamic variable is determined at run-time. Based on the value assigned to the dynamic variable the appropriate type is determined at run time (also known as late-binding).

1:      dynamic d = "Hello";
2:      string type = d.GetType().ToString(); //System.String
3:   
4:      d = false;
5:      type = d.GetType().ToString(); //System.Boolean

The above code compiles successfully. In fact even this will compile:

1:      dynamic dmic = 555;
2:      dmic.DoSomethingOnThisNumber();

But of course at run time it will identify it as System.Int and give error: “RuntimeBinderException: 'int' does not contain a definition for 'DoSomethingOnThisNumber'”Dynamic can also be used in un-initialized fields and method parameters. So both of uses below are legal:

1:  void Main()
2:  {
3:      dynamic i; //No Error
4:  }
5:   
6:  void Test(dynamic i) //No Error
7:  {
8:      
9:  }

Shorts - week 3, 2022

Post with links to what I am reading: 1. A very good post on different aspects of system architecture: https://lethain.com/introduction-to-a...