background preloader

10 C# Shorthands that improve productivity

10 C# Shorthands that improve productivity
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting! One of the best things about C# is that as the language and libraries expand thought is put into keeping things readable. Below I have listed 10 shorthands that you can use to make your code tighter and less wordy. No doubt you know one or more already — but do you currently use all ten of them ? 1. I first read about the ? 1.int x = 10; 2.int y = 20; 3.int max; 5.if (x > y) 6. max = x; 7.else 8. max = y; Using the (Question) ? 3.int max = (x > y) ? 2. How often do you test for null values in your code? 1.object cache = null; 2.object d = new object(); 3.object e; 5.if (c ! 6. e = c; 8. e = d; It is obvious that we can rewrite this using the single ? 3.object e = (c ! Using the ?? 3.object e = c ?? 3. After you create a new object you often have to assign one or more of its properties. 1.Customer c = new Customer(); 2.c.Name = "James"; 4.c.Address = "204 Lime Street"; can be written as: 4. 03.try 07.finally 10.if (font1 ! 11.

Spring.NET - Application Framework General, Debugging, Options Dialog Box To access the General page, open the Tools menu and choose Options. In the Options dialog box, expand the Debugging node and choose General. This page lets you set the following general debugging options: Ask before deleting all breakpoints Requires confirmation before completing the Delete All Breakpoints command. Break all processes when one process breaks Simultaneously breaks all processes to which the debugger is attached, when a break occurs. Break when exceptions cross AppDomain or managed/native boundaries In managed or mixed-mode debugging, the common language runtime can catch exceptions that cross application domain boundaries or managed/native boundaries when the following conditions are true: 1) When native code calls managed code by using COM Interop and the managed code throws an exception. 2) When managed code running in application domain A calls managed code in application domain B and the code in application domain B throws an exception. Enable address-level debugging

VS 2010 and .NET 4 Series [In addition to blogging, I have been using Twitter more recently to-do quick posts and share links. You can follow me on Twitter at: (@scottgu is my twitter name)] Over the next few months I’m going to be doing a series of posts that talk about some of the cool things coming with the VS 2010 and .NET 4 release. VS 2010 and .NET 4 are the next major releases of our developer tools and framework. I will update this page with links to the individual posts I do as I publish them along the way: Hope this helps, Scott

Compiled » String Formatting in C# I couldn’t find a quick reference to .NET string formatting using the String.Format() function, so I created this one (which has also spawned this String Formatting FAQ). When I started working with the .NET framework, one thing puzzled me. I couldn’t find sprintf(). sprintf() is the C function that takes an output buffer, a format string, and any number of arguments, and builds a string for you. char szError[256];sprintf(szError, “Error %d occurred. This would write “Error 12 occurred.” into the szError buffer (assuming nError was 12). str << “Error ” << nError << ” occurred.” << endl; Or something close to that. The .NET framework handles strings very nicely – but it takes some getting used to. string errorString = String.Format(“Error {0} occurred.”, nError); Teeming with metadata, the .NET environment doesn’t need the format string to say what type of data you’re formatting, just where you want it. Strings There really isn’t any formatting within a string, beyond it’s alignment. Numbers

Related: