Random .NET Tips & Tricks: Using, LINQ, Initializers, and Config Encryption [VB.NET]
I've just started my summer internship at General Mills and while I can't talk a lot about the different things I'll be working on or have seen (on pain of death), I can at least share with you the random tips I've picked up through my .NET training and work.
Are You Using Using?
A neat little VB.NET/C# statement that I never knew much about was the Using keyword. What does it do? It works with any type that implements the IDisposable interface. When the End Using statement is hit, .NET will immediately clean up the object, rather than marking it as "trash this" which is what it normally does .NET calls the Dispose() method of the object. Update: A helpful soul clarified exactly what Using is for and why it's nice. Using is useful because it has a cleaner syntax and you don't have to use Finally blocks to check if your object is Nothing to dispose it. For more discussion on VB.NET's Using, check out this post and Hexar's comment showing the typical syntax pre-Using.
Here's an example (note: pseudocode):
Using conn As New SqlConnection(blah)
conn.Open()
Dim cmd As New SqlCommand(blah)
Using rdr As IDataReader = cmd.ExecuteReader()
While rdr.Read()
' Do stuff
End While
End Using ' Disposes DataReader
End Using ' Disposes conn
It's not an amazing thing, but it will trim some of your code and make it easier to recognize blocked code.
Check out this C# article on Using statements.
LINQ Tips
This is VB-specific, but when making a LINQ statement and you want to return the full result set, you do not need the select statement (pseudocode):
Dim q = from p in db.Products _
where p.Price > 5
You can also return new objects using anonymous types, in case you want to create a custom object that only uses what you need (pseudocode):
Dim q = from p in db.Products _
where p.Price > 5 _
select new { _
.SKU = "SKPID" & p.ProductID & "M" & p.Manufacturer, _
.Price = p.Price }
Object Initializers
I didn't know much about the new object initialization method introduced in C# 3.0 and VB9 until today. A lot of people pass in properties through the constructor of an object when initializing it. This has multiple shortcomings. What if you wanted to only provide one property? You could have an if statement or pass in Nothing… but soon this would become a lot of extra work.
Using this method, here's how you would create a fake entity called "Person" using typical properties:
Dim newPerson As New Person ' no constructor newPerson.Name = "Harold" newPerson.Age = 25 newPerson.City = "Footown" arrPersons.Add(newPerson) newPerson = New Person newPerson.Name = "blahblahblah" ' Repeat ad infinitum
So, there's got to be a better way, right? Right! You can use the With statement and with it (heh) you can easily and quickly initialize new objects.
arrPersons.Add(New Person With { _
.Name = "Harold", _
.Age = 25, _
.City = "FooTown" })
arrPersons.Add(New Person With { _
.Name = "blahblahblah" })
' Repeat
' Note: regular initialization using With
Dim newPerson As New Person With { .Name = "Foo" }
Check out this screencast and article about the topic.
Encrypt Your Web.Config
I never knew about this but apparently you can encrypt sections of your web.config file. Why would you do this, can't the public already not access it? Yes, but your programmers, administrators, and employees can. Sometimes you need to put in an extra level of protection, for example to protect database connections, and in that case, you should think about encrypting your connection strings section.
Tip: .NET will encrypt sections that are both in the web.config file and config files referenced by web.config (for example, "connections.config").