Friday, August 8, 2008

Better Coded example for Dictionaries in C#

Here is the post on Channel8.msdn blog about how to handle deletion from dictionaries in C#. Click here to follow the link..

Friday, August 1, 2008

Peek into the (notsofar) Future - C# 4.0

C# has evolved into a highly functional language, with even Operating Systems being written in it, the C# design team at Microsoft Research has been working hard to press the new level programming ideology into the C# mould.

I saw the C# developers interview on the channel9 and the goals that they are targeting are
  • More declarative programming
  • Making the Language more dynamic (heard something like Global Functions)
  • Concurrency management (watch the video for a great discussion on this one
They are basically looking forward to the multi-core future which could be certainly asymmetric. Catch the video for this one..

Tuesday, July 8, 2008

Some Funny C#??

args of string many are they) Main is what they seek yet return they do not.



Brace you must

Written it is, the Console. “Hello World”

Check out this hilarious post..

Sunday, April 20, 2008

Handling File Size when transferring over the network..

In my recent project, I used sockets in C# to transfer a file from one host to another over a LAN environment. The socket creation exercise was pretty easy, but how to manage the file transfer??

My problem was that I decided to use an Asynchronous model for my Server, in that, the Server would be able to connect to many hosts at the same time. This gave rise to the need to handle multiple "buffers" at the same time and independent of each other. But C#(or rather .Net) to the rescue, all the async code is smoothly handled, but the logic behind keeping tab of the file name and file size needed some thought..

1. How to transfer the file name over the network?

- When sending the file over the network, we might face (more often that not) a situation where we are supposed to assume that the file name at the target system will be same as the file name on the native host. To take care that the file name is preserved, I made use of the "pre-data" buffer in the send file method, highlighted in red below.

public void SendFile(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags);

We can use this field to transmit the file name across the network along with the first data packet.

Wednesday, April 16, 2008

Creating sockets in C#

Creating sockets in c# is a simple affair, but managing the async calls can be a big pain..
my idea was to connect two boxes through sockets and do some simple file transfer.

The first thing is the code for creating the sockets:

//create the server socket..
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

Now the address family here is the type of addressing scheme that you are going to use..
SocketType.Stream specifies that the data will be sent as a stream of bytes and the ProtocolType is TCP here.
You can even use UDP and many others..

By default the socket receive and send buffer size is 8bytes.. havent tested this on many systems but this was the case on my personal and work pc both.
This is very helpful while stripping off data in chunks that has been sent as a blocks of data packed in a single buffer.

I'll talk more about the buffers soon.

Thursday, March 27, 2008

Q. What do you use C# for? Ans. Writing OSes

"...it is impossible to predict how a singularity will affect objects in its causal future." - NCSA Cyberia Glossary

Yes, the Singularity Project at Microsoft Research has proved radical in more than one ways, the most significant digression from OS coding tradition being the fact that the OS platform has been coded in a Very High Level Language - C#!!

Actually, its written in Sing#, an extension of C# that first-class support for OS communication primitives as well as strong support for systems programming. The first question in anyones mind would be performance on C# which is a JIT compiled language requiring a significant overhead in comparison to the likes of C and C++, but according to Microsoft Research, use of safe programming languages eliminates many preventable defects, such as buffer overruns.

Second, the use of sound program verification tools further guarantees that entire classes of
programmer errors are removed from the system early in the development cycle.

Third, an improved system architecture stops the propagation of runtime errors at well-defined boundaries, making it easier to achieve robust and correct system behavior.

In their laboratory trial builds, the research team juggled between using Safe and Unsafe code for critical elements like Garbage Collector. It would be very interesting to see how it affects the performance and especially what security loop-holes may be induced with unsafe code...

On an ending note, when I was reading about Singularity, the research team has clearly conveyed that their target is simplicity of design rather than performance. Quite a paradigm shift I would say.

Read the whole Singularity series, starting here..
DIGITAL AND TECH: Microsoft Singularity - Series I

Tuesday, January 29, 2008

Using Set & Get properties in C#

Using Set and Get properties in C# is really cool. You can basically do two things to data while using properties:
  • Make data Read-only, Write-Only or Read-Write both.
  • Make the data Source - Transparent to the user of the application.
In this post, I will show how to make the Read-Only using "get" property!

We start with an example:
Consider the following array -

private List m_OxyEntities = new List( );
//This line of code creates a new list, which is the actual store of the data.



public List OxyEntities
{
get
{
return m_OxyEntities;
}
}

//This line of code creates another list, but when accessing any element of the list, there is automatic mapping to the corresponding element of the actual list create above using the "
Get - Return" combination.

Basically, since we allow only "get" and expose only the list OxyEntities (remember its public, while actual data list is private), we make it a read only list when being accessed out of the scope.

I will talk about making it Write only in the next post.