Wednesday, February 13, 2008

Preserve Ownership, mode and other attributes while copying in Linux

I dare not say, I am savvy at Linux :-)

Recently, I ran into a problem where I found that upon executing a copy command, my source and destination files had different permissions and attributes (i-nodes), but I badly needed the permissions to be preserved in the destination files/folders. The following command will preserve the attributes -

cp -p source destination

here, the switch p (-p) does the trick, it preserves the permissions, modes and other attributes to resemble the source.

Hope this helps some of you with similar needs!

Tuesday, February 05, 2008

C# keyword 'yield return' - I find it interesting

I have been using yield return in some of my codes and would like to share its interesting aspects. The following code example shows one dummy use of 'yield return'-


public class YieldExample
{

public void Run()
{
IEnumerator numbers = getNumbers().GetEnumerator();
while (numbers.MoveNext())
{
Console.Write("x{0} ", numbers.Current);
}
}

private IEnumerable getNumbers()
{
for (int i = 0; i <>

Now, a code like new YieldExample().Run() would produce a output similar to this -
 "x0 y0 x1 y1 x2 y2 x3 y3 x4 y4 x5 y5 x6 y6 x7 y7 x8 y8 x9 y9"  
This output is certainly a little unusual but this is what 'yield return' is used for.

In a line, whenever you place a yield return statement, the control goes back to its caller. However, on the next call, program execution starts from the line immediate to the 'yield return' line. This can be used to avoid initializing/memorizing a collection of data if it makes sense to use them one after another and requires to move to control back and forth the iterator.

Monday, February 04, 2008

C# 3.0 Anonymous types - actual type depends on the order of the property assignnments

I learned that the anonymous types in C# 3.0 has got a good feature that, if you have two anonymous declarations with the same signature, then you create only one anonymous type and have two instances of the type.

So, a code fragment like the following is perfectly valid and passes all compiler warnings/errors-

var anon = new { FirstName = "first", Age = 21 };
var anon2 = new { FirstName = "first", Age = 26 };

//this is OK
anon = anon2;

Now, lets take a look into the following code fragment-

var anon = new { FirstName = "first", Age = 21 };
var anon3 = new { Age=16, FirstName = "first2"};

//this doesn't compile
anon = anon3;

So, its evident that the anonymous types are created on the order of the assignments of the properties. However, from my little experience and understanding, I think if this dependency on the exact order wasn't there, then it would make more sense.