-3

Possible Duplicate:
Use of var keyword in C#

  1. What is the benefit of using var for initializing an object as in the first line of following code, var sp is used for SerialPort.

  2. What is the benefit of disposing an object after using it? As in following code, the object SerialPort is disposed after an SMS has been sent to the recipient.

Code:

using (var sp = new SerialPort(cbcomport.Text))
{
    sp.Open();
    sp.WriteLine("AT" + Environment.NewLine);
    sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
    sp.WriteLine("AT+CMGS=\"" + dt.Rows[i]["PhoneNo"] + "\"" + Environment.NewLine);
    sp.WriteLine(tbsms.Text + (char)26);
    Thread.Sleep(5000);
}
Community
  • 1
  • 1
kashif
  • 3,713
  • 8
  • 32
  • 47

3 Answers3

4

1) With var you don't have to write out the word SerialPort twice.

If you're calling a method that returns an object that might have a very complicated signature, using var definitely helps. The most common case is for linq queries.

var q = from c in orders
        order by c.id
        select c;

The type of q is IOrderedQueryAble<Order> which I prefer to not write out.

2) Disposing the serial port when you're done with it immediately closes it and releases it for other applications to use it. If you don't dispose it, the port will be locked until the GC has collected the object (which may take quite some time).

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • what about the second question??? – kashif Apr 30 '12 at 10:51
  • @kashif it's answered too. Read again. – wRAR Apr 30 '12 at 10:51
  • does disposing free the memory it occupied and terminates the object??? – kashif Apr 30 '12 at 10:51
  • @kashif no, `Dispose` only frees unmanaged resources. And don't shout. – wRAR Apr 30 '12 at 10:56
  • Thanks Ander Abel now I understood it... Thanks Alot – kashif Apr 30 '12 at 10:57
  • @wRAR: `Dispose` does nothing, as it is defined in an interface. If your class implements IDisposable, and your class holds managed resources (such as a SqlConnection), and you release these during the call to `Dispose`... Or unhooks event listeners... then it is freeing managed resources. –  Dec 02 '12 at 23:27
4
  1. With var you type less.

  2. The GC will be free to collect the disposed object when it sees fit. If it is an unmanaged resource, the call to Dispose will cleanup those resources.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • what is unmanaged resource. is it something like that an object occupies the space in memory when created and disposing it after using it = closing it to free the memory or somthing else?? – kashif Apr 30 '12 at 10:54
  • @kashif - The parallel port is one. Other can be: file systems, databases. Basically, any code that is not .NET or that uses resources that are not .NET (COM objects, ports, buses etc...) – Oded Apr 30 '12 at 10:56
1

In your case var is an option of not writing SerialPort twice like

SerialPort sp = new SerialPort(cbcomport.Text)

and

var sp = new SerialPort(cbcomport.Text)

Both of the above statements are same

moreover in some places like LINQ when we are not sure what the outcome of LINQ query is then we write var to store fetched results.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208