views:

124385

answers:

303
+1410  Q: 

Hidden Features of C#?

This came to my mind after I learned the following from this question:

where T : struct

We, C# developers, all know the basics of C#. I mean declarations, conditionals, loops, operators, etc.

Some of us even mastered the stuff like Generics, anonymous types, lambdas, LINQ, ...

But what are the most hidden features or tricks of C# that even C# fans, addicts, experts barely know?

Here are the revealed features so far:


Keywords

yield by Michael Stum
var by Michael Stum
using() statement by kokos
readonly by kokos
as by Mike Stone
as / is by Ed Swangren
as / is (improved) by Rocketpants
default by deathofrats
global:: by pzycoman
using() blocks by AlexCuse
volatile by Jakub Šturc
extern alias by Jakub Šturc

Attributes

DefaultValueAttribute by Michael Stum
ObsoleteAttribute by DannySmurf
DebuggerDisplayAttribute by Stu
DebuggerBrowsable and DebuggerStepThrough by bdukes
ThreadStaticAttribute by marxidad
FlagsAttribute by Martin Clarke
ConditionalAttribute by AndrewBurns

Syntax

?? (coalesce nulls) operator by kokos
number flaggings by Nick Berardi
where T:new by Lars Mæhlum
implicit generics by Keith
one-parameter lambdas by Keith
auto properties by Keith
namespace aliases by Keith
verbatim string literals with @ by Patrick
enum values by lfoust
@variablenames by marxidad
event operators by marxidad
format string brackets by Portman
property accessor accessibility modifiers by xanadont
conditional (ternary) operator (?:) by JasonS
checked and unchecked operators by Binoj Antony
implicit and explicit operators by Flory

Language Features

Nullable types by Brad Barker
Currying by Brian Leahy
anonymous types by Keith
__makeref __reftype __refvalue by Judah Himango
object initializers by lomaxx
format strings by David in Dakota
Extension Methods by marxidad
partial methods by Jon Erickson
preprocessor directives by John Asbeck
DEBUG pre-processor directive by Robert Durgin
operator overloading by SefBkn
type inferrence by chakrit
boolean operators taken to next level by Rob Gough
pass value-type variable as interface without boxing by Roman Boiko
programmatically determine declared variable type by Roman Boiko
Static Constructors by Chris
Easier-on-the-eyes / condensed ORM-mapping using LINQ by roosteronacid

Visual Studio Features

select block of text in editor by Himadri
snippets by DannySmurf

Framework

TransactionScope by KiwiBastard
DependantTransaction by KiwiBastard
Nullable<T> by IainMH
Mutex by Diago
System.IO.Path by ageektrapped
WeakReference by Juan Manuel

Methods and Properties

String.IsNullOrEmpty() method by KiwiBastard
List.ForEach() method by KiwiBastard
BeginInvoke(), EndInvoke() methods by Will Dean
Nullable<T>.HasValue and Nullable<T>.Value properties by Rismo
GetValueOrDefault method by John Sheehan

Tips & Tricks

nice method for event handlers by Andreas H.R. Nilsson
uppercase comparisons by John
access anonymous types without reflection by dp
a quick way to lazily instantiate collection properties by Will
JavaScript-like anonymous inline-functions by roosteronacid

Other

netmodules by kokos
LINQBridge by Duncan Smart
Parallel Extensions by Joel Coehoorn

+146  A: 

"yield" would come to my mind. Some of the Attributes like [DefaultValue()] are also among my favorites.

The "var" keyword is a bit more known, but that you can use it in .net 2.0 Applications as well (as long as you use the .net 3.5 Compiler and set it to output 2.0 code) does not seem to be known very well.

Edit: Thanks kokos for pointing out the ?? operator, that's indeed really useful. Since it's a bit hard to google for it (as ?? is just ignored), here is the Documentation Page for that operator: http://msdn.microsoft.com/en-us/library/ms173224.aspx

Michael Stum
The default value's documentation says it is not really setting the value of the property. It's only a helper for visualisers and code generators.
borisCallens
As for DefaultValue: In the meantime, some libraries use it. ASP.net MVC uses DefaultValue on Parameters of a Controller Action (Which is very useful for non-nullable types). Strictly speaking of course, this is a code generator as the value is not set by the compiler but by MVC's code.
Michael Stum
The name for the ?? operator is the "Null Coalescing" operator
Atømix
+71  A: 
  1. ?? - coalescing operator
  2. using (statement / directive) - great keyword that can be used for more than just calling Dispose
  3. readonly - should be used more
  4. netmodules - too bad there's no support in Visual Studio
kokos
"using - great keyword that can be used for more than just calling Dispose"I am intrigued, can you elaborate on that?
Loki Stormbringer
using can also be used to alias a long namespace to a more convenient string, i.e.:using ZipEncode = MyCompany.UtilityCode.Compression.Zip.Encoding;There's more here:http://msdn.microsoft.com/en-us/library/sf0df423.aspx
Dave R.
It really isn't the same thing. When calling Dispose, you may use the using statement, when aliasing types you are using a using directive.
Øyvind Skaar
The `using` block has the syntax `using(expr)block` which means, that you don't have to store the IDisposable resource in a local variable.You can abuse this feature to enforce arbitrary methods to be called at the end of the block: `using(Writer.Indent()) Writer.WriteLine("x");`
SealedSun
Just in case you'd like a name to #1 (as you did with the ternary operator), ?? is called the null coalescing operator.
J. Steen
Definitely with you on #4. I have started using netmodules for test demos of code that I keep in one big DEV project, and then want to incorporate them in my proof-of-concept project without having 100 different dlls for all those bits.
maxwellb
It really sucks that you can't google ??
Lucas McCoy
@LucasAardvark: As J Steen mentioned it's called the null coalescing operator. Search for that!
kokos
"??" is very handy.
Armin Ronacher
To search for ?? operator at google try:http://www.google.com/search?q=c%23+%3F%3F+operator
backslash17
+21  A: 

Two of my personal favourites, which I see rarely used:

  1. Snippets (particularly for properties, which was made even better for 2008)
  2. The ObsoleteAttribute
DannySmurf
I like the switch snippet very much. Makes switching on an enum sooo much easier ;)
OregonGhost
i regularly use a snippet for Properties calling OnPropertyChanged in the setter. very handy.
Botz3000
Are snippets though a feature of Visual Studio? Rather than C#/compiler?
maxwellb
snippets are part of visual studio shortcut is ctrl - k+ctrl x
Maslow
better yet eg: write "for" or "switch" and then double-press 'tab' key
murki
very handy indeed both with Ctrl-K + Ctrl-X and double tab on "for", "while", "foreach", "if", etc.
Chenster
+49  A: 

Honestly the experts by the very definition should know this stuff. But to answer your question:

http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx

The compiler flagging for numbers are widely known for these:

Decimal = M
Float = F
Double = D

// for example
double d = 30D;

However these are more obscure:

Long = L
Unsigned Long = UL
Unsigned Int = U
Nick Berardi
Every time I'm dealing with decimals, I have to look up the m. Is it only me or is m not really intuitive? :)
OregonGhost
The M syntax comes from the old VB type called Money. M == Money == Decimal.
Nick Berardi
is there one for byte?
Maslow
Nope, anything less than an Int32 is automatically inferred by the compiler based on the type to the left of it
Nick Berardi
@Nick: nice - I like learning the historicals behind the code.
dboarman
+207  A: 

I didn't know the "as" keyword for quite a while.

MyClass myObject = (MyClass) obj;

vs

MyClass myObject = obj as MyClass;

The second will return null if obj isn't a MyClass, rather than throw a class cast exception.

Mike Stone
Don't over-do it though. Lots of people seem to use as because the prefer the syntax even though they want the semantics of a (ToType)x.
Scott Langham
Yes, what's specified in this answer is only one of the differences between (ToType) and "as ToType".
bzlm
The "as" keyword also offers better performance :-)
Dan Herbert
I don't believe it offers better performance. Have you profiled it? (Obviously though it does when the cast fails... but when you use (MyClass) cast, failures are exceptional.. and extremely rare (if they happen at all), so it makes no difference.
Scott Langham
This is only more performant if the usual case is the cast failing. Otherwise the direct cast (type)object is faster. It takes longer for a direct cast to throw an exception than to return null though.
Spence
Right along the same lines of the "as" keyword... the "is" keyword is just as useful.
dkpatt
I don't consider this a "hidden" feature.
Dave Van den Eynde
It should be noted that this only works on nullable classes. You have to use int? instead of int.
phsr
You can abuse it and have a NullReferenceException down your road later when you could have had a InvalidCastException earlier.
Andrei Rinea
@spence: +10. Many people don't realize that if the cast is more than likely to succeed, using 'as' will reduce performance compared to a direct cast.
SnOrfus
@SnOrfus @Spence have you profiled the performance of the two cast methods in a recent CLR version? I didn't see any performance difference between the two when they succeeded.
James Schek
+55  A: 
  • TransactionScope and DependentTransaction in System.Transactions is a lightweight way to use transaction processing in .NET - it's not just for Database transactions either
  • String.IsNullOrEmpty is one that I am surprised to learn a lot of developers don't know about
  • List.ForEach - iterate through your generic list using a delegate method

There are more, but that is the three obvious ones of the top of my head...

KiwiBastard
I found that TransactionScope aggressively promotes transactions to distributed mode which uses DTC. When DTC becomes involved you'll probably have to deal with DCOM security. I tend to avoid the pain by using native transactions.
Hans Malherbe
Any proof that List.ForEach can be faster than normal loop?
arbiter
That List.ForEach is faster than foreach or for(;;) is completely bonkers. ForEach uses a method/function delegate to implement the behavior. This is first of all, means worse cache locality because the code is generally executed further away (in memory) from the actual loop. Secondly all you really need to do to verify that this is slower is to look a the generated native code. There's a lot more stuff going on with List.ForEach than you might think.
John Leidegren
+1 for String.IsNullOrEmpty. I use it all the time.
Bernhof
Thanks, I always hated to distinguish whether a string is empty or null. This looks cool.
Braveyard
Completely bonkers? Well, I found .ForEach to be faster than all other options. See http://jerrytech.blogspot.com/2010/02/revisiting-c-loop-performance.html if you doubt me. Always doubt me ;) The code's there - run it yourself and see.
JerryNixon
On the other hand, Patrick Smacchia says the opposite: ForEach is slower than For. http://codebetter.com/blogs/patricksmacchia/archive/2008/11/19/an-easy-and-efficient-way-to-improve-net-code-performances.aspx
James
+137  A: 

Attributes in general, but most of all DebuggerDisplay. Saves you years.

Stu
Using DebuggerDisplay Attribute (MSDN): http://msdn.microsoft.com/en-us/library/x810d419.aspx
Mufasa
Jumping Jehosophet!
George Mauer
"DebuggerDisplay" replaced with a link to an usefull MSDN article.
boj
+134  A: 

I tend to find that most C# developers don't know about 'nullable' types. Basically, primitives that can have a null value.

double? num1 = null; 
double num2 = num1 ?? -100;

Set a nullable double, num1, to null, then set a regular double, num2, to num1 or -100 if num1 was null.

http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx

Brad Barker
whats the difference between this and just using the wrapper objects fro primatives?
shsteimer
some types are nullable normally, like DateTime. So, DateTime? is a godsend
Simucal
DateTime cannot be set to null.
Jason Jackson
This is effectively C# syntactic sugar for Nullable<T>, so is merely possibly by use of generics.
Wim Hollebrandse
So then is "int" just C# syntactic sugar for System.Int32 ? There actually is compiler support built around the Nullable types, enabling setting them to null, for instance (which can't be done using generic structs alone), and boxing them as their underlying type.
P Daddy
@P Daddy - yes, int is syntactic sugar for System.Int32. They are completely interchangeable, just like int? === Int32? === Nullable<Int32> === Nullable<int>
ck
@ck: Yes, int is an alias for System.Int32, as T? is an alias for Nullable<T>, but it isn't *just* syntactic sugar. It's a defined part of the language. The aliases not only make the code easier to read, but also fit our model better. Expressing Nullable<Double> as double? underscores the perspective that the value thus contained is (or may be) a double, not just some generic structure instantiated on type Double.(continued)
P Daddy
... In any case, the argument wasn't about aliases (that was just a poor analogy, I suppose), but that nullable types—using whatever syntax you prefer—are indeed a language feature, not just a product of generics. You can't reproduce all the functionality of nullables (comparison to/assignment of null, operator forwarding, boxing of underlying type or null, compatibility with null coalescing and `as` operators) with generics alone. Nullable<T> alone is rudimentary and far from stellar, but the concept of nullable types as part of the language is kick ass.
P Daddy
had no clue data types that were once hated by null can now work as a null. :D Great to know!
Mike
A: 

Currying using

FastFunc<T,U>
Brian Leahy
How is this a C# feature? I thought this was F#? Can you explain more?
bennage
This is not a currying sample. Please see Mads Torgersen's blog on this.
eriawan
How is this currying? Please provide an example.
Mark Good
I still done see what this is? Can someone explain?
miguel
All the example shows is a function with two generics, not too helpful. Need a detailed example.
Paul Fryer
+6  A: 

I have often come across the need to have a Generic parameter-object persisted into the viewstate in a base class.

public abstract class BaseListControl<ListType,KeyType,ParameterType>
                 : UserControl 
                 where ListType : BaseListType
                 && ParameterType : BaseParameterType, new
{

    private const string viewStateFilterKey = "FilterKey";

    protected ParameterType Filters
    {
        get
        {
            if (ViewState[viewStateFilterKey] == null)
                ViewState[viewStateFilterKey]= new ParameterType();

            return ViewState[viewStateFilterKey] as ParameterType;
        }
        set
        {
            ViewState[viewStateFilterKey] = value;
        }
    }

}

Usage:

private void SomeEventHappened(object sender, EventArgs e)
{
    Filters.SomeValue = SomeControl.SelectedValue;
}

private void TimeToFetchSomeData()
{
    GridView.DataSource = Repository.GetList(Filters);
}

This little trick with the "where ParameterType : BaseParameterType, new" is what makes it really work.
With this property in my baseclass, I can automate handling of paging, setting filter values to filter a gridview, make sorting really easy, etc etc.

Really just saying that generics can be an enormously powerful beast in the wrong hands.

Lars Mæhlum
Maybe I'm being daft, but I'm not sure I understand the code. Could you post a more complete example?
ilivewithian
Its pretty simple actually. If you have a GridView with a couple of DropDownLists to filter out content, you just put the values into your filter object that is persisted between postbacks, and send it as a parameter to the method that fetches data from the DB.You would just implement your UserControl inheriting from BaseListControl, and the base takes care of persisting "state" between postbacks.
Lars Mæhlum
what do you mean "little trick"? Are you referring to the fact that you can't create a parameterised type inside a generic unless the generic constraints contains a "new" clause?
Andy Dent
No, the little trick is constraining to BaseParameterType. A bit of a typo :\
Lars Mæhlum
+7  A: 

@Horsey: You're right, I thought of that after I posted. I am no pro here and am still learning, so I do make the occasional bad post here and there.

@Mike: The as keyword is great, and also lends itself to the use of the "is" keyword:

 MyClass c;
 if (obj is MyClass)
      c = obj as MyClass

I'm sure that this is no secret, but I did not find this for at least 6 months into learning C#.

Ed Swangren
Will this end up converting obj to MyClass twice if it is of type MyClass? If so would one conversion (using as) and a check for null be any faster?
Karg
Why not: MyClass c = obj as MyClass; if (c) { // your logic }
Matt Cruikshank
@Matt Cruikshank that would produce a compiler error, you need if(c!=null)
Greg Dean
i think it is redundant to use both is and as, pick one.
CiNN
agree with Matt - better to use as and check for null
Dmitri Nesteruk
Don't need the "as" because you're checking the type :=> if(obj is MyClass) c = (MyClass)obj;
Atømix
Yes, I know. This is redundant and not how I would do things today, but I never changed it =)
Ed Swangren
I think this kind of code is good, as it makes the intention very clear, i.e. if this object IS of this type, then cast AS this type.
Eyvind
I can't believe I did not stumble across this before. Thanks so much!
Adrian Grigore
+77  A: 

@Ed, I'm a bit reticent about posting this as it's little more than nitpicking. However, I would point out that in your code sample:

MyClass c;
  if (obj is MyClass)
    c = obj as MyClass

If you're going to use 'is', why follow it up with a safe cast using 'as'? If you've ascertained that obj is indeed MyClass, a bog-standard cast:

c = (MyClass)obj

...is never going to fail.

Similarly, you could just say:

MyClass c = obj as MyClass;
if(c != null)
{
   ...
}

I don't know enough about .NET's innards to be sure, but my instincts tell me that this would cut a maximum of two type casts operations down to a maximum of one. It's hardly likely to break the processing bank either way; personally, I think the latter form looks cleaner too.

Dogmang
You know - believe it or not - the direct cast is *slower* than the as cast. I didn't believe this until I measured it myself.
Kent Boogaart
If the cast is to the exact type (cast to "A" when object is "A", not derived from it), the straight cast is ~3x FASTER than "as". When casting a derived type (cast to "A" when object is "B", which derives from "A"), the straight cast is ~0.1x slower than "as". "is", then "as" is just silly.
P Daddy
FxCop will warn you if you place a 'as' inside an 'if is'.
Jonathan C Dickinson
not knowing C#, but borrowing from c++, does this work?if(MyClass c = obj as MyClass) { ... }
Will
Will: That won't work in c#, because null and false cannot be compared to each other.
Neil Whitaker
No, but you could write "if ((c = obj as MyClass) != null)".
Dave Van den Eynde
@P Daddy: as Kent said, as casting is faster anyway. I've also benchmarked this and found this to be the case.
Ben Collins
`is` and `as` won't do user casts. So, the above code is asking with the `is` operator if obj is derived from MyClass (or has an implicit system defined cast). Also, `is` fails on `null`. Both of these edge cases may be important to your code. For instance, you may want to write: `if( obj == null || obj is MyClass ) c = (MyClass)obj;` But this is strictly different from: `try { c = (MyClass)obj; } catch { }` since the former will not perform any user defined conversions, but the latter will. Without the `null` check, the former will also not set `c` when `obj` is `null`.
Adam Luter
@Adam Just wanted to point something out. In your first example, `if( obj == null || obj is MyClass ) c = (MyClass)obj;` you might as well just use `as`, since it's clearer, has slightly better performance characteristics, and (if I'm reading it correctly) performs the same work. I believe it's FxCop that points out that `is` performs an `as`-like cast under the covers, and it flags that double-cast–style as bad practice.
WCWedin
+208  A: 

All the above, plus

1) implicit generics (why only on methods and not on classes?)

void GenericMethod<T>( T input ) { ... }

//Infer type, so
GenericMethod<int>(23); //You don't need the <>.
GenericMethod(23);      //Is enough.

2) simple lambdas with one parameter:

x => x.ToString() //simplify so many calls

3) anonymous types and initialisers:

//Duck-typed: works with any .Add method.
var colours = new Dictionary<string, string> {
    { "red", "#ff0000" },
    { "green", "#00ff00" },
    { "blue", "#0000ff" }
};

int[] arrayOfInt = { 1, 2, 3, 4, 5 };

Another one:

4) Auto properties can have different scopes:

public int MyId { get; private set; }

Thanks @pzycoman for reminding me:

5) Namespace aliases (not that you're likely to need this particular distinction):

using web = System.Web.UI.WebControls;
using win = System.Windows.Forms;

web::Control aWebControl = new web::Control();
win::Control aFormControl = new win::Control();
Keith
I've actually needed to do the first one on a project using Anthem, because it clobbers the WebControls namespace.
Adam Lassek
Yeah, I find lots of 3rd-party libraries tend to do that.
Keith
in #3 you can do Enumerable.Range(1,5)
Echostorm
Yeah, you can for that specific example, maybe it was poorly chosen. You can init any array in this way: int[] arrayOfInt = new { 4, 8, 15, 16, 23, 42 }
Keith
i think you've been able to initialize arrays withint[] nums = {1,2,3};since 1.0 :) doesn't even need the "new" keyword
Lucas
You can, but now you can also do this: var lst = new List<int> { 4, 8, 15, 16, 23, 42 }
Keith
+1 for Dictionary and List initializer
chakrit
+1 for different scopes for accessors
George Mauer
also lambda without parameters ()=> DoSomething();
pablito
Just found out about 5) recently, but u beat me to it. Good one.
chaiguy
That feature helped me a LOT recently.
JohannesH
+1 for implicit generics....I used them...but didnt realize :-)
Peter Gfader
I weep for auto properties not getting default values
Chad Grant
@3) int[] arrayOfInt = new { 1, 2, 3, 4, 5 };I even more like var[] intArray=new {1,12,14};@4) Unfortunately, only 'get;private set;' works, any other combination like 'get; internal set;' is not possible :O(Better than nothing but not perfectly consistent IMHO, though this is rather syntactical sugar and no fundamental language ability
Simpzon
I've used both { get; internal set; } and { get; protected set; }, so this pattern is consistent.
Keith
+1 for #4. Goofing around in SO before work really pays off :)
Kei
@Keith: +1 for this correction, I was mislead by someone stating this. In fact, one can chose various access modifiers for either get or set. The real restriction is that you cannot use one for get AND one for set (makes sense because then one should change the access modifier of the property instead of the accessors)
Simpzon
+1 for #4 as well
es11
The namespaces aliases remind me of the yucky c++ :-S
Andrei Rinea
Ooo, I like this application of Enumerable.Range() which looks cleaner: new way:______________foreach (int i in Enumerable.Range(1, 100)){ Console.WriteLine(i);}old way:_______________for (int i = 1; i <= 100; i++){ Console.WriteLine(i);}
BrokeMyLegBiking
@BrokeMyLegBiking - actually I would keep the `for` loop in that case - the `foreach` would be slower and more confusing. The `Enumerable.Range` is most useful for initialising collections without a countered loop. If you're going to loop anyway it's best to stick with `for`
Keith
or web.Control aWebControl = new web.Control();
Kirk Broadhurst
@Kirk Broadhurst - you're right - `new web.Control()` would also work in this example. The `::` syntax forces it to treat the prefix as a namespace alias, so you could have a class called `web` and the `web::Control` syntax would still work, while the `web.Control` syntax wouldn't know whether to check the class or the namespace. Because of that I tend to always use `::` when doing namespace aliases.
Keith
this last namespace trick is really useful to override an existing one.See on my blog for example! http://alsagile.com/archive/2010/03/09/stop-the-war-between-nunit-and-mstest-make-them.aspx
Stephane
You can have as many parameters to your lambdas as you'd like:(n1,n2,n3) => n1+n2+n3;
L. Kolmodin
@L. Kolmodin - yeah you can - my 2nd point is that when you just have 1 you can skip the parameters: `(x) => ` becomes `x => `
Keith
//Duck-typed: works with any .Add method.var colours = new Dictionary<string, string> { { "red", "#ff0000" }, { "green", "#00ff00" }, { "blue", "#0000ff" }};int[] arrayOfInt = { 1, 2, 3, 4, 5 };this is not duck typing!
lukas
@lukas - this is syntactic sugar, but under the hood it works on any class that implements a method like `void Add(...)` - that includes your own classes without the need to implement an interface. I agree that on the `int[]` it's C# syntax, but that it works for any type with an Add method is duck-typing
Keith
+117  A: 

Here's a useful one for regular expressions and file paths:

"c:\\program files\\oldway"
@"c:\program file\newway"

The @ tells the compiler to ignore any escape characters in a string.

Patrick
Also, a @ constant accepts newlines inside. Perfect when assigning a multiline script to a string.
Tor Haugen
Don't forget also to escape a quotation mark just double them up, in other words. [code]var candy = @"I like ""red"" candy canes.";[/code]
Dave
I tend to build paths with Path.Combine. I definitely use the @ for regex!
phsr
@new is also a variable instead of a keyword: @this, @int, @return, @interface...so on :)
dboarman
+5  A: 

@Brad Barker

I think if you have to use nullable types, it's better to use Nullable<.T> rather than the question mark notation. It makes it eye-achingly obvious that magic is occurring. Not sure why anyone would ever want to use Nullable<.bool> though. :-)

Krzysztof Cwalina (one of the authors of Framwork Design Guidlines) has a good post here: http://blogs.msdn.com/kcwalina/archive/2008/07/16/Nullable.aspx

And Mike Hadlow has a nice post on Nullability Voodoo

IainMH
Not sure I agree with Mike Hadlow. Sounds like someone who has spent a long time using languages and dbs without good null support and is a little 'afraid' of changing. Use of null in business terms is usually obvious. It means "don't know", or "doesn't have one" (i.e. "doesn't have an end date").
cbp
"Not sure why anyone would ever want to use Nullable<.bool> though"How about when you are interacting with BIT columns from an SQL Server database? A BIT datatype in T-SQL can return 1, 0 or NULL and can be directyly converted to a Nullable<Bool> by .NET Nullables are extremely useful when dealing with data in databases, as "value types" can actually be NULL.
Dan Diplo
@cbp and @Dan Dilpo - You could argue that if you're allowing nulls in a value type like a bool you should be using something else.
IainMH
+96  A: 

Here are some interesting hidden C# features:

__makeref

__reftype

__refvalue

These are undocumented C# keywords (even Visual Studio recognizes them!) that were added to for a more efficient boxing/unboxing prior to generics. They work in coordination with the System.TypedReference struct.

There's also __arglist, which is used for variable length parameter lists.

One thing folks don't know much about is System.WeakReference -- a very useful class that keeps track of an object but still allows the garbage collector to collect it.

The most useful "hidden" feature would be the yield return keyword. It's not really hidden, but a lot of folks don't know about it. LINQ is built atop this; it allows for delay-executed queries by generating a state machine under the hood. Raymond Chen recently posted about the internal, gritty details.

Judah Himango
+1 for the weakreference, but more info would be nice.
Peter Gfader
+1 this is basically the only answer that really provides "hidden" features, not features that "I did not know about yesterday so I thought they were hidden from me"
Marek
+91  A: 

Not sure why anyone would ever want to use Nullable<bool> though. :-)

True, False, FileNotFound?

Michael Stum
that should be "Brillant"
Kevin
if expect a user to answer a yes no question then null would be appropriate if the question has not been answered
Omar Kooheji
I used a nullable bool when populating a ddl of employees.Active only = true. InActive only - false. Active and Inactive = null
Dining Philanderer
Omar is on the right track--imagine a web form with two radio buttons "Yes" and "No" but no default selection. If the user submits the form without clicking either button, you have a null choice.Also could be useful for something like "Did we succeed, fail, are we still waiting for the result?"
John Zwinck
I actually use that in a similar fashion in a class that serves as a filter. You can set a bool? to true/false, or just leave it as null to say "Don't filter by that column". But still, someone HAD to bring up the FileNotFound one, especially after someone else laid the groundwork already.
Michael Stum
Nullable types are handy for interaction with a database where table columns are often nullable.
tuinstoel
Do i have to worry that i had to laugh?
Rauhotz
Miss Bean i presume.
JohannesH
Yes, No, Maybee?
Dan Blair
Store values of a ThreeState CheckBox
Shimmy
Yes, No, Dunno
Andrei Rinea
As in SQL: Yes/no/unknown.
erikkallen
Using enum is a better approach instead of using nullable boolean
JCasso
Usually yes, especially as enums are speaking for themselves. However, enums are yet another type, and adding new types is not always a good idea. As usual, it depends.
Michael Stum
One day i was thinking about this and meanwhile our company announced a new Health Insurance scheme and Forms where given to all employee to fill and in the bottom of the form i saw a Question "Are You pregnant?" and Options were "Yes" OR "NO" i was shocked to find out how a male can respond to this. So I made a New Option Box in Form with Title NA(Not Applicable) and marked that one. From that day i realize why we need Nullable<bool> :).And I my view while development there are many times you code paths never answers against your boolean flag and that is the exact place you need bool?
Mubashar Ahmad
Utterly diffident, one fails to answer Yes and No question with a Maybe.
Chenster
@erikkallen you mean 1 / 0 / NULL, a Nullable bool field.
Shimmy
Nullable<bool> doesn't make sence to me either, as a boolean is "Yes/No", there is no maybe...
Dusty Roberts
@Dusty There are some examples in the comments. NULL usually means "I don't know" or "Not set/No Decision has been made". Think of creating a database filter where you want to allow the user to show only items where a bool is TRUE, FALSE or both/ignore the filter.
Michael Stum
@Mubashar null is not maybe, not applicable or similar it by definition means "no information" as in I don't know what the value mean since it has no meaning. Modeling three state checkboxes and other three state with bool? should rather be implemented as enum or other three state representation
Rune FS
A: 

Not sure why anyone would ever want to use Nullable<.bool> though. :-)

True, False, FileNotFound?

@Michael Stum - That is brilliant :-)

IainMH
+5  A: 

In no particular order:

Lists<>
Mutex

The new property definitions shortcut in Framework 3.5.

Diago
+234  A: 

Avoid checking for null event handlers

Adding an empty delegate to events at declaration, suppressing the need to always check the event for null before calling it is awesome. Example:

public delegate void MyClickHandler(object sender, string myValue);
public event MyClickHandler Click = delegate {}; // add empty delegate!

Let you do this

public void DoSomething()
{
    Click(this, "foo");
}

Instead of this

public void DoSomething()
{
    // Unnecessary!
    MyClickHandler click = Click;
    if (click != null) // Unnecessary! 
    {
        click(this, "foo");
    }
}

Please also see this related discussion and this blog post by Eric Lippert on this topic (and possible downsides).

Andreas H.R. Nilsson
Great tip. Just got done using this myself!
xanadont
Interesting tip! Personally, I use helper extension methods from my project (http://www.codeplex.com/thehelpertrinity) to simply do this:Click.Raise(this, "foo");
Kent Boogaart
superb - that will save some typing
Chris Ballard
I believe a problem will appear if you rely on this technique and then you have to serialize the class. You will eliminate the event, and then on deserialization you will get a NullRefference.... .So one can just stick to the "old way" of doing things. It's safer.
sirrocco
you can still set your event handler to null, so you can still get a null reference, and you still have a race condition.
Robert Paulson
Also, performance-wise, it has to call an empty method every time.
TraumaPony
I really don't like that this approach gets so much attention. The performance hit you take is actually somewhat substantial.
senfo
The performance hit of an exception that would be generated by a null reference would be greater than an empty delegate. I like the tip. Of course, doing an if-check on the delegate would be the fastest, but its still an elegant solution to a common problem.
dviljoen
If I understand it correctly, this forces any subscribed-to events to be multicast instead of singlecast, with a big performance hit as a result. It also forces an invoke on events that would otherwise not have to be called, another big hit. Not worth the tiny bit of typing it saves!
P Daddy
A quick profile test shows that dummy-subscribed event handler without null test takes roughly 2x the time of unsubscribed event handler with null test. Multicast event handler without null test takes about 3.5x the time of singlecast event handler with null test.
P Daddy
I don't know why this looks so elegant, looks like a hack to me...for that same reason, then why not assign all objects to something, instead of checking to null before using them? Besides, I agree with P Daddy, the performance hit is not worth the savings in typing.
Ricardo Villamil
This avoids the need for a null check by just always having a self-subscriber. Even as an empty event this carries an overhead that you don't want. If there are no subscribers you don't want to fire the event at all, not always fire an empty dummy event first. I would consider this bad code.
Keith
This is a great tip. To the developers that like checking every reference for null, STOP IT! Only check a reference for null if you expect that it might be null FOR A GOOD REASON!
Sean
This one is money. I saw it a while back. It's a real nug.
Chuck Conway
Doh!! Why didn't I think of that??? Excellent, thanks, +1!
Charlie Flowers
Can someone with rep modify the post, to include information about the performance cost? Not everyone reads the comments.
drhorrible
This is a terrible suggestion, for the reasons in the above comments. If you must make your code look "clean", use an extension method to check for null then call the event. Someone with modify privileges should definitely add the cons to this answer.
Greg
You shouldn't modify the answer without very good reason. Downvote the answer if you believe it's no good. Others obviously think it is.
Josh Smeaton
It's also suspect because a class user might set the event to null, removing your "self-subscribed" handler and causing issues. And the "instead of" code was bad. That, at least, I could just fix.
Joel Coehoorn
Also see this post for more details and alternatives: http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx
Joel Coehoorn
I prefer using extension methods for event firing, I use a slightly modified (for increased thread safety) version of the technique at http://elegantcode.com/2007/12/05/firing-events-with-extension-methods/
Phil Price
the "you don't want to fire it at all" argument is BS. The check should clearly be confined to the event infrastructure, and not the user. It is a language misdesign, which should rather be fixed. Empty subscriber list is a valid subscriber list! Zero is a number, and black is a color.
Pavel Radzivilovsky
Pavel is right in saying that this should never have been an issue in the first place. This should have been designed into the language. What possible reason is there for an event to be null? That said, adding an empty delegate is a less-than-ideal solution. An extension method is better, but it is still best-practice to create an invoker method, so that you can more easily control access to the event. For example, if you decide to allow child classes to raise the event, you can simply make the invoker method protected. The extension method should aid in writing invokers--not replace them.
StriplingWarrior
+448  A: 

This isn't C# per se, but I haven't seen anyone who really uses System.IO.Path.Combine() to the extent that they should. In fact, the whole Path class is really useful, but no one uses it!

I'm willing to bet that every production app has the following code, even though it shouldn't:

string path = dir + "\\" + fileName;
ageektrapped
I'll take you on your bet
Greg Dean
Everyone should be using Path.Combine()... it's crazy not to.
Jon Tackabury
I use Path all the time, it's a life saver :-)
TheCodeJunkie
I even recreated the most usefull parts of it in php
Kris
Path.Combine is also essential for cross-platform coding as it uses whatever path separator the current OS uses.
Gary Willoughby
I always use it. And I always have to correct my coworkers' code...
Tor Haugen
Path.Combine is one of my favorite methods, I use it all the time.
Loki Stormbringer
Whipping up a Url.Combine method is also very useful.
cbp
Path.Combine( ) is really bad, it doesn't combine paths!Do MessageBox.Show(Path.Combine(@"c:\temp", @"\assembly")); and you will see why. See also http://dotnet.org.za/hiltong/archive/2008/02/19/incorrect-results-with-path-combine.aspx
tuinstoel
Not everyone likes System.IO.Path, see http://codebetter.com/blogs/patricksmacchia/
tuinstoel
Mine doesn't, I rape Path. Although I would like a little more functionality.
Jonathan C Dickinson
@Jonathan C Dickinson, but does your Path.Combine() work? See above!
tuinstoel
I wish for a Path class to have strong typed instances of a path; much like the Uri class.
Øyvind Skaar
wow, never heard of this; thanks!
p.campbell
@tuinstoel it works for me so far :-) Prefixing a path with a slash should have the same effect as when you execute cd \ in command prompt, it should take you back to the root no matter where you are.... so Path.Combine is working as it should...
chakrit
@chakrit, Path.Combine should never ignore the first parameter, it would be better if it throws an exception when the second parameter starts with an \. I don't use it.
tuinstoel
I was thrilled when I stumbled upon Path.Combine(). It makes life so much easier!
dkpatt
It's a great function. I'm happy not having to do all the backslash- checking manually.
Botz3000
Now if only it would take in a param array of items instead of just two..
Pondidum
tuinstoel, it doesn't ignore the first parameter, the leading slash on the second parameter tells it to remove everything but the drive from it. Combining "c:\test" and "..\assembly" will do the same thing,
Blorgbeard
Garbage in, Garbage out... input validation! If you don't want users to be able to do stuff, then don't let them. Don't complain that a function is written as generically as possible to allow the greatest variety of use.
Matthew Scharley
I've been using the Path class exclusively. Mostly for Mono.
Kawa
that is really cool
es11
I know this method. useful! :)
Anwar Chandra
"Now if only it would take in a param array of items instead of just two.." Couldn't you write an extension method?
J.13.L
Params support is coming in .NET 4.0. Thankfully
ageektrapped
It's great if your data is clean. It's useless if you're trying to maintain an application with dirty data. This isn't garbage in, garbage out, this is dirt in, garbage out. Dirt can be dealt with, Path.Combine() simply doesn't do it, for absolutely no good reason.
Mike Burton
@Mike, learn the reason and suddenly you will discover it is a good reason indeed.
Jimbo
@Mike Burton Strange because for me, if Path.Combine worked otherwise, it'd be garbage.
chakrit
I use it every time I need to build a path. Its might be the most useful class in BCL
Ismail
@Isamil: more useful than `Object`?
Joe
Joe. Absolutely! What does `Object` do? Pretty much nothing! What does `Path` do? Lots of stuff with paths! There's no contest! :-)
Ken
+142  A: 

The @ tells the compiler to ignore any escape characters in a string.

Just wanted to clarify this one... it doesn't tell it to ignore the escape characters, it actually tells the compiler to interpret the string as a literal.

If you have

string s = @"cat
             dog
             fish"

it will actually print out as (note that it even includes the whitespace used for indentation):

cat
             dog
             fish
lomaxx
wouldn't the string include all the spaces that you used for indentation?
andy
Yes it's called verbatim string.
Joan Venge
this is cool, thanks for this.
goldenratio
It would be clearer if the output showed the spaces that would be printed out as well. Right now it seems as if the new lines characters are printed but spaces are ignored.
aleemb
+1 I new about @ to ignore escape characters but I did not realize you could do multiline strings. Nice.
J.Hendrix
+171  A: 

Two things I like are Automatic properties so you can collapse your code down even further:

private string _name;
public string Name
{
    get
    {
        return _name;
    }
    set
    {
        _name = value;
    }
}

becomes

public string Name { get; set;}

Also object initializers:

Employee emp = new Employee();
emp.Name = "John Smith";
emp.StartDate = DateTime.Now();

becomes

Employee emp = new Employee {Name="John Smith", StartDate=DateTime.Now()}
lomaxx
This makes classes much quicker to write.
David Basarab
Should it be noted that Automatic Properties are a C# 3.0 only feature?
Jared Updike
The great thing is typing "Prop" 2x[tab] and you get the property for free :)
borisCallens
Automatic Properties were introduced with the 3.0 compiler. But since the compiler can be set to output 2.0 code, they work just fine. Just don't try to compile 2.0 code with automatic properties in an older compiler!
Josh
Something many people don't realise is that get and set can have different accessibility, eg:public string Name { get; private set;}
Nader Shirazie
Only problem with Automatic Properties is that it doesn't seem to be possible to provide a default initialization value.
Glytzhkof
NValidate doesn't like automatic properties...
Mike Kingscott
@Glytzhkof : yes it is, see `[DefaultValue()]` in http://stackoverflow.com/questions/9033/hidden-features-of-c/9035#9035 ( http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx )
ANeves
...I always provide default values for Automatic Properties in my constructor. Will look into the `DefaultValue` attribute!
Funka
@ANeves : no it's not. From that page: **A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. You must set the initial value in your code.**`[DefaultValue]` is used for the designer so it knows whether to show a property in bold (meaning non-default).
Roger Lipscombe
@Roger Lipscombe: thanks plenty for the correction. You are absolutely right - http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
ANeves
About the default value: you could write a base class that sets the default prop values from the DefaultValueAttribute via reflection in the constructor, but there would be a performance loss obviously
Guillaume86
+19  A: 

On the basis that this thread should be entitled "things you didn't know about C# until recently despite thinking you already knew everything", my personal feature is asynchronous delegates.

Until I read Jeff Richter's C#/CLR book (excellent book, everyone doing .NET should read it) I didn't know that you could call any delegate using BeginInvoke / EndInvoke. I tend to do a lot of ThreadPool.QueueUserWorkItem calls (which I guess is much like what the delegate BeginInvoke is doing internally), but the addition of a standardised join/rendezvous pattern may be really useful sometimes.

Will Dean
Yes BeginInvoke/EnInvoke uses the threadpool, I learned that in this forum. See http://stackoverflow.com/questions/442991/c-iasyncresult-and-the-thread-pool and http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx .
tuinstoel
"things you didn't know about C# until recently despite thinking you already knew everything" +1
corymathews
+154  A: 

The 'default' keyword in generic types:

T t = default(T);

results in a 'null' if T is a reference type, and 0 if it is an int, false if it is a boolean, etcetera.

Eric Minkes
Cool! I have to remember this!
Dave Van den Eynde
Plus: type? as a shortcut for Nullable<type>. default(int) == 0, but default(int?) == null.
Markus
+11  A: 

Lambda Expressions

Func<int, int, int> add = (a, b) => (a + b);

Obscure String Formats

Console.WriteLine("{0:D10}", 2); // 0000000002

Dictionary<string, string> dict = new Dictionary<string, string> { 
    {"David", "C#"}, 
    {"Johann", "Perl"}, 
    {"Morgan", "Python"}
};

Console.WriteLine( "{0,10} {1, 10}", "Programmer", "Language" );

Console.WriteLine( "-".PadRight( 21, '-' ) );

foreach (string key in dict.Keys)
{
    Console.WriteLine( "{0, 10} {1, 10}", key, dict[key] );    
}
David in Dakota
Lambda Expressions are explained more fully here: http://www.developer.com/net/csharp/article.php/3598381 and here: http://msdn.microsoft.com/en-us/library/bb397687.aspx
Ogre Psalm33
+9  A: 

I love the fact that I can use LINQ to objects on plain old .NET 2.0 (i.e. without requiring .NET 3.5 to be installed everywhere). All you need is an implementation of all the query operator Extension methods - see LINQBridge

Duncan Smart
+1 LINQBridge is terrific
Maslow
+7  A: 

In addition to duncansmart's reply, also extension methods can be used on Framework 2.0. Just add an ExtensionAttribute class under System.Runtime.CompilerServices namespace and you can use extension methods (only with C# 3.0 of course).

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute
    { 
    }
}
Serhat Özgel
+28  A: 

Being able to have enum types have values other than int (the default)

public enum MyEnum : long
{
    Val1 = 1,
    Val2 = 2
}

Also, the fact that you can assign any numeric value to that enum:

MyEnum e = (MyEnum)123;
lfoust
But the values have to be discreet.So no floats or doubles etc.Just for completeness' sake ;)
borisCallens
Why would you want to be able to assign just any old value to an enum? Isn't the point of an enum to limit the choices of values?
RobH
I believe the compiler has to support this for the sake of supporting flags. So given the enum above, if you do MyEnum val = MyEnum.Val1 | MyEnum.Val2 you would end up with a value that is outside of the already defined possible values. (in this case 3). Since you can do binary arithmetic on enums they can theoretically have many possible values.
lfoust
One good reason would be to match it to the ID column of a read-only bootstrap table in a database.
Aaronaught
Also, note that you can assign `MyEnum e = (MyEnum)123;`, but when you access e you'll get an Exception.
ANeves
You can also have [Flags] to tell that the enum is flag based
SztupY
@RobH- I've found it useful in unit testing when needing to test some conditional logic and force it down a branch of an if-else (or switch) statement by using an invalid enum value.
Ray Vega
@RobH - Say you have a local enum that maps to an enum of an external code, of an API for example. If a new value is added to the external type and you haven't updated your enum def, at least you can store the numeric value. Good for reference.
Sean
+278  A: 

From CLR via C#:

When normalizing strings, it is highly recommended that you use ToUpperInvariant instead of ToLowerInvariant because Microsoft has optimized the code for performing uppercase comparisons.

I remember one time my coworker always changed strings to uppercase before comparing. I've always wondered why he does that because I feel it's more "natural" to convert to lowercase first. After reading the book now I know why.

John
Wow, thats a great one!
AviD
When you "convert a string to upper case" you create a second temporary string object.I thought that this kind of comparison was not preferred, that the best way was: String.Equals(stringA, stringB, StringComparison.CurrentCultureIgnoreCase)whcih does not create this throwaway string at all.
Anthony
What kind of optimization can you perform on comparing upper case strings that can't be done on lower case strings? I don't understand why one would be more optimal than the other.
Parappa
Ran a quick test. Compared "XXX" to "XxX" 1 million times, three ways (no conversion, invariant, and string.Equals). Here are the results:No Match Test: Got 0 matches in 1 msInvariant Match Test: Got 100000 matches in 23 msstring.Equals Match Test: Got 100000 matches in 8 ms
dviljoen
I just use string.Compare(strA, strB, true) for a culture invariant-non case sensitive comparison, no need to convert both string to upper (it's probably done by the compiler much more efficiently).
Ricardo Villamil
Converting to uppercase rather than lowercase can also prevent incorrect behavior in certain cultures. For example, in Turkish, two lowercase i's map to the same uppercase I. Google "turkish i" for more details.
Neil Whitaker
@neilwhitaker1: That is not an issue when using *invariant* conversions.
Rasmus Faber
I tried benchmarking ToUpperInvariant vs ToLowerInvariant. I cannot find any difference in their performance under .NET 2.0 or 3.5. Certainly not anything that warrant "highly recommending" using one over the other.
Rasmus Faber
Don't question the mighty Jeffrey Richter! :P Although for probably 90% of applications by SO developers this will make a tiny difference.
Chris S
ToUpperInvariant is preferred because it makes all characters round-trip.See http://msdn.microsoft.com/en-us/library/bb386042.aspx.For comparisons, write`"a".Equals("A", StringComparison.OrdinalIgnoreCase)`
SLaks
I think turkey i is the only reason to be concerned here, most likely your call to ToUpper is not in a critical path and worrying about it would be premature optimization. If you found out it was, you can easily fix it then. However, since this affects the turkey i problem, I would insist on using ToUpper even if it were slower.
Adam Luter
@neilwhitaker1: it's not true that in turkish both i's map the same uppercase letter, they both map to different upper case letters. So I don't see why uppercase would be better then lowercase.
Paulo Manuel Santos
in Turkish, mapping is (i -> İ) and (ı -> I). So "files".ToUpper() != "FILES" and similarly "FILES".ToLower() != "files". That's why you need to use ToUpperInvariant() or ToLowerInvariant() in certain cases.
ssg
+16  A: 

My favourite is the

global::

keyword to escape namespace hell with some of our 3rd party code providers...

pzycoman
example -> http://msdn.microsoft.com/en-us/library/c3ay4x3d(VS.80).aspx
bob
You should have written some examples as well.
Braveyard
Could do with an example
Shahin
Yep, need a sample of how to use this please.
Paul Fryer
+9  A: 

I didn't start to really appreciate the "using" blocks until recently. They make things so much more tidy :)

AlexCuse
+85  A: 

This one is not "hidden" so much as it is misnamed.

A lot of attention is paid to the algorithms "map", "reduce", and "filter". What most people don't realize is that .NET 3.5 added all three of these algorithms, but it gave them very SQL-ish names, based on the fact that they're part of LINQ.

"map" => Select
Transforms data from one form into another

"reduce" => Aggregate
Aggregates values into a single result

"filter" => Where
Filters data based on a criteria

The ability to use LINQ to do inline work on collections that used to take iteration and conditionals can be incredibly valuable. It's worth learning how all the LINQ extension methods can help make your code much more compact and maintainable.

Brad Wilson
Select force you to always return something from the supplied expression wherears map is less picky about that.
chakrit
Select also acts like the "return" function in monads. See http://stackoverflow.com/questions/9033/hidden-features-of-c#405088
Mark Cidade
Using "select" is only required if you use the SQLish syntax. If you use the extension method syntax -- someCollection.Where(item => item.Price > 5.99M) -- the use of select statements isn't required.
Brad Wilson
@Brad, that (where) is a filter operation. Try doing a map operation without select...
Eloff
LINQ is the big thing that happened to C# in my opinion: http://stackoverflow.com/questions/2398818/whats-the-next-big-thing-after-linq-closed
Leniel Macaferi
+107  A: 

Returning anonymous types from a method and accessing members without reflection.

// Useful? probably not.
private void foo()
{
    var user = AnonCast(GetUserTuple(), new { Name = default(string), Badges = default(int) });
    Console.WriteLine("Name: {0} Badges: {1}", user.Name, user.Badges);
}

object GetUserTuple()
{
    return new { Name = "dp", Badges = 5 };
}    

// Using the magic of Type Inference...
static T AnonCast<T>(object obj, T type)
{
   return (T) obj;
}
dpp
That's insanely cool.
Kent Boogaart
Very nice! I thought the only way to return multiple values was to define a new type.
blackwing
That really doesn't get you anything. It is actually dangerous. What if GetUserTuple is modified to return multiple types? The cast will fail at run time. One of the great things about C#/.Net is the compile time checking. It would be much better to just create a new type.
Jason Jackson
@Jason I did say it's probably not useful but it is surprising (and I thought hidden).
dpp
While cool, this seems like a rather poor design choice. You've basically defined the anonymous type in two places. At that point, just declare a real struct and use it directly.
Paul Alexander
cool, but have to be very very careful with.
Shimmy
I could see this being useful if you have some sort of convention running throughout your application when you return tuples
George Mauer
@George: such a convention would be called a... struct?
Martinho Fernandes
Very cool... +1
J.13.L
I think any potential use there may have been for this has probably been replaced by the `dynamic` keyword. If you're going to play with fire, you might was well do it the easy way. ;-)
StriplingWarrior
this trick is named 'cast by sample' and it will not work if method that returns anonymous type is located in another assembly.
desco
@desco: "hidden features" not necessarily useful.
dpp
@StriplingWarrior: Actually, anonymous types and dynamic go together quite well, so you cannot really say one replaces the other - if that is what you were suggesting. (i.e. with dynamic you could get rid of the cast entirely and just do `dynamic user = GetUserTuple();` and the rest would stay the same...)
kloffy
@kloffy: yes, that's exactly what I mean. `dynamic` doesn't replace anonymous types: it replaces the need for this AnonCast method in order to access members without reflection.
StriplingWarrior
@StriplingWarrior: My bad, I misinterpreted your comment. I absolutely agree that the `dynamic` version is to be preferred (maybe it should be added to this answer as an alternative)...
kloffy
+18  A: 

There's also the ThreadStaticAttribute to make a static field unique per thread, so you can have strongly typed thread-local storage.

Even if extension methods aren't that secret (LINQ is based on them), it may not be so obvious as to how useful and more readable they can be for utility helper methods:

//for adding multiple elements to a collection that doesn't have AddRange
//e.g., collection.Add(item1, item2, itemN);
static void Add<T>(this ICollection<T> coll, params T[] items)
 { foreach (var item in items) coll.Add(item);
 }

//like string.Format() but with custom string representation of arguments
//e.g., "{0} {1} {2}".Format<Custom>(c=>c.Name,"string",new object(),new Custom())
//      result: "string {System.Object} Custom1Name"
static string Format<T>(this string format, Func<T,object> select, params object[] args)
 { for(int i=0; i < args.Length; ++i)
    { var x = args[i] as T;
      if (x != null) args[i] = select(x);
    }
   return string.Format(format, args);
 }
Mark Cidade
Wow man, you just blew my mind. That's sweet.
jcollum
See also http://stackoverflow.com/questions/271398/post-your-extension-goodies-for-c-net-codeplexcomextensionoverflow?answer=358259#358259
Mark Cidade
+1 I was about to answer with generic extensions methods, but this answer does that plus more :)
Si
+1  A: 

I think if you have to use nullable types, it's better to use Nullable<.T> rather than the question mark notation. It makes it eye-achingly obvious that magic is occurring. Not sure why anyone would ever want to use Nullable<.bool> though.

In a VB.Net Web-Service where the parameter might not be passed through (because the partners request wasn't consistent or reliable), but had to pass validation against the proposed type (Boolean for "if is search request"). Chalk it up to "another demand by management"...

...and yes, I know some people think it's not the right way to do these things, but IsSearchRequest As Nullable(Of Boolean) saved me losing my mind that night!

Pat
+107  A: 

Using @ for variable names that are keywords.

var @object = new object();
var @string = "";
var @if = IpsoFacto();
Mark Cidade
Why would you want to use a keyword as a variable name? Seems to me that this would make code less readable and obfuscated.
Jon
This is pure, unadulterated EVIL I tell you! :-)
Carl Camera
Well, the reason that it's there is that CLI requires it for interoperability with other languages that might use C# keywords as member names
Mark Cidade
marxidad's reason is good. After that, Carl is right. If I had to maintain code where somebody did this, I can't be held responsible for what happens the next time I see them! :)
Matt Blaine
If you ever wanted to use the asp.net MVC HTML helpers and define a HTML class you will be happy to know that you can use @class so it won't be recognised as the class keyword
borisCallens
If I find that in anyone's code in production, that person is Going To Hell.
endian
Great for extension methods. public static void DoSomething(this Bar @this, string foo) { ... }
Jonathan C Dickinson
Great tip, with several legitimate uses! :)
Vojislav Stojkovic
This is not a feature, @ is just allowed symbol for naming like _ and many others (name '@this' will not be equal to name 'this')
zihotki
There is a P/Invoke importer that does that if the signatures use C# keywords as parameter names. Looks strange, but makes the import more or less literal.
OregonGhost
@zihotki: Wrong. var a = 5; Console.WriteLine(@a);Prints 5
SLaks
This especially useful for generated code, where you can influence the attr-names manually. The @attr approach is already used by some XSD-import functionalities for C#/.NET.
Juve
It can also be used to signify that some variable is used as a SqlParameter, var @pMyParameter = 1; new SqlParameter("@pMyParameter",@pMyParameter);
John Leidegren
Personally, i think `Class @class` is better than `Class clazz`
Rob Fonseca-Ensor
I often use `@return` for the return variable, `@default` for a default value, `@this` for the first parameter of an extension method, etc. When I use them like this I think it really helps the clarity of the code - especially for extension methods.
Enigmativity
I have a class which has field named "from" and i didn't think that this name can cause a problem but it cause in linq statement because from keyword is belongs to linq statements. Now i learned how to deal with it.
Freshblood
+43  A: 

Events are really delegates under the hood and any delegate object can have multiple functions attached to it and detatched from it using the += and -= operators, respectively.

Events can also be controlled with the add/remove, similar to get/set except they're invoked when += and -= are used:

public event EventHandler SelectiveEvent(object sender, EventArgs args) 
  { add 
     { if (value.Target == null) throw new Exception("No static handlers!");
       _SelectiveEvent += value;
     }
    remove
     { _SelectiveEvent -= value;
     }
  } EventHandler _SelectiveEvent;
Mark Cidade
I had no idea. Cool!
Greg
+20  A: 

It's not actually a C# hidden feature, but I recently discovered the WeakReference class and was blown away by it (although this may be biased by the fact that it helped me found a solution to a particular problem of mine...)

Juan Manuel
+2  A: 

I must admit that i'm not sure wether this performs better or worse than the normal ASP.NET repeater onItemDatabound cast code, but anyway here's my 5 cent.

MyObject obj = e.Item.DataItem as MyObject;
if(obj != null)
{
  //Do work
}
martinlund
+72  A: 

If you're trying to use curly brackets inside a String.Format expression...

int foo = 3;
string bar = "blind mice";
String.Format("{{I am in brackets!}} {0} {1}", foo, bar);
//Outputs "{I am in brackets!} 3 blind mice"
Portman
Those are braces, not brackets. These are brackets: [ ]
Kyralessa
@Kyralessa: Actually, yes, they are braces, but "curly brackets" is an alternate name for them. `[` and `]` are square brackets, and `<` and `>` are angle brackets. See http://en.wikipedia.org/wiki/Bracket.
icktoofay
You are correct. But when I commented, it didn't say "curly" brackets.
Kyralessa
Very nice one to point out. Remember my first String.Format looked like:String.Format("{0}I am in curly brackets{1} {2} {3}", "{","}", 3, "blind mice"); Once I found out that escaping these is done by using {{ and }} I was so happy :)
Gertjan
Muahahahaa... Programming already 3 years in .Net and I didn't know this one. :D
Arnis L.
+20  A: 

Near all the cool ones have been mentioned. Not sure if this one's well known or not

C# property/field constructor initialization:

var foo = new Rectangle() { Fill = new SolidColorBrush(c), 
                            Width = 20, 
                            Height = 20 };

This creates the rectangle, and sets the listed properties.

I've noticed something funny - you can have a comma at the end of the properties list, without it being a syntax error. So this is also valid:

var foo = new Rectangle() { Fill = new SolidColorBrush(c), 
                            Width = 20, 
                            Height = 20, };
mgsloan
The comma at the end makes fiddling with the values much easier :)
OregonGhost
The trailing comma is also useful for generated code. You'll note that it holds for many situations. I run across it most often when making an enum. :)
Greg D
Enums also support the trailing comma "feature".
SkippyFire
You don't need the () in Rectangle() either
rball
+28  A: 

Not hidden, but I think that a lot of developers are not using the HasValue and Value properties on the nullable types.

        int? x = null;
        int y;
        if (x.HasValue)
            y = x.Value;
Rismo
How would one employ a nullable type without using HasValue?
Cheeso
Like this:int? x;if(x != null)
Rismo
No, people like to write: y = x ?? defaultvalue.
Dave Van den Eynde
Just to be clear, (x != null) and (x.HasValue) result in identical IL.
Snarfblam
I prefer `x != null` over `x.HasValue`.
ANeves
x!= null is preferable in a generic method where the method may receive either a non-nullable or a nullable. In either case, casting as object will allow you to get at the value without .HasValue which the non-nullable value doesn't have.
BenAlabaster
+21  A: 

@lomaxx I also learned the other day (the same time I learned your tip) is that you can now have disparate access levels on the same property:

public string Name { get; private set;}

That way only the class itself can set the Name property.

public MyClass(string name) { Name = name; }
xanadont
This syntax was added to c# in 2.0. C++ allowed it on .Net 1.0 and 1.1
Bert Huijben
Unfortunately this is the only applicable combination of distinct access rights, 'get; internal set;' would not work
Simpzon
How do you figure? This compiled for me fine just now:public string Foo { get; internal set; }
xanadont
protected set; is really useful for base classes.
Arnis L.
What would the point of a private property be anyway? what's wrong with 'this.Name='?
Bryan
+352  A: 

From Rick Strahl:

You can chain the ?? operator so that you can do a bunch of null comparisons.

string result = value1 ?? value2 ?? value3 ?? String.Empty;
John
cool !! but don't you think you might mess evrything up ?
Yassir
Nice! Definitely cleaner than 3 if statements!
SkippyFire
`??` is insanely useful for making your code readable. Unfortunately because C# is strongly typed, it insists that all of the operands be of the same type. This can make it's use in operator chaining annoying. Otherwise, a great operator.
Adam Luter
I didn't know this. thanks! :)
Anwar Chandra
MGOwen
@MGOwen, that's the equivilent of saying "Take the first non-null value in this list." string result will be value1 if value1 isn't null. If it is, it will use value2, etc.
Matt Grande
This is an OR comparision or AND comparision?
Ismail
@Ismail S, @MGOwen: It's not a comparison, it's instead of writing this code;string result = String.Empty;if (value1 != null) result = value1;else if (value2 != null) result = value2;else if (value3 != null) result = value3;
L. Kolmodin
That means, it will give first not null value out of value1, value2, value3 and String.Empty if all the three are nulls? Is that correct?
Ismail
@Ismail S yes .
corymathews
Thank you very much.
Ismail
Nice. i always thought it would be great to have something like T-SQL's COALESCE in C# (for an arbitrarily long set of values.) This is close enough. Thanks for the tip!
Paul Sasik
Do we have similar operator in javascript?
Ismail
+20  A: 

Partial Methods

Charlie Calvert explains partial methods on his blog

Scott Cate has a nice partial method demo here

  1. Points of extensibility in Code Generated class (LINQ to SQL, EF)
  2. Does not get compiled into the dll if it is not implemented (check it out with .NET Reflector)
Jon Erickson
I dunno, Partial methods have always seemed like a code smell to me...
George Mauer
In a lot of cases perhaps. Good for separating generated code from developer code though.
BlackWasp
I use this to produce multiple versions of an assembly. Some versions are endowed with extra magic capabilities, and some are not. I embed the methods that perform the magic in a separate code module, and mark them partial. Then, I can call them from the primary code module, without worrying which version of the assembly it is, and without #if conditionals.
Cheeso
I use it just the way BlackWasp does;PartialMethod++ == Delegate-- == Speed * 2;
Behrooz
I didn't know you can tag the partial keyword to a method, interesting... Still I don't think I'll use it... yet. I do rely heavily on partial for auto generated classes, but *this* implies that the auto generated classes aren't complete until I've filled in the partial method. I wonder whether there wouldn't have been a better way to write the app which generates the classes.
Christo
+65  A: 

Maybe not an advanced technique, but one I see all the time that drives me crazy:

if (x == 1)
{
   x = 2;
}
else
{
   x = 3;
}

can be condensed to:

x = (x==1) ? 2 : 3;
JasonS
If I'm not mistaken, this has been around since some of the early days of C++. (Straight C probably had it too but I can't recall.)
lc
"This" is called the "ternary operator". http://en.wikipedia.org/wiki/Ternary_operation
Portman
The ternary operator has been banned in my group. Some people just don't like it, though I've never understood why.
fatcat1111
I guess because they're not the same things, being an operator not a statement. I prefer the first approach myself. It's more consistent and easily expandable. Operators can't contain statements, so the minute you have to expand the body, you'll have to convert that ternary operator to an if statement
kervin
can be condensed tox++;Ok it's pointless ^^
François
@Guillaume: To account for all values of x: x = 2 + System.Math.Min(1,System.Math.Abs(x-1));
mbeckish
It's actually called the "conditional operator" - it is *a* ternary operator because it takes three arguments. http://en.wikipedia.org/wiki/Conditional_operator
Blorgbeard
@kervin: conditional operator **can** contain statements, if its 2nd and 3rd parts both have the same type. E.g.: `(t == null) ? "Null" : t.GetType().Name` contains statement in the 3rd part.
Roman Boiko
But it can't be used if statement is required, because conditional operator is just an expression with some value, and not a statement. See also: http://stackoverflow.com/questions/1816859/using-conditional-operator-in-lambda-expression-in-foreach-on-a-generic-list/1816984#1816984
Roman Boiko
that is very useful. thanks! I love these tips
Mike
We call this an in line if.
runrunraygun
@Roman those aren't statements, just expressions.
siride
@siride Thanks. There are multiple definitions of statements vs expressions which got me confused. Some useful explanations: http://msdn.microsoft.com/en-us/library/ms173143.aspx (switch to C# tab), and http://stackoverflow.com/questions/19132/expression-versus-statement/19224#19224
Roman Boiko
+7  A: 

Preprocessor Directives can be nifty if you want different behavior between Debug and Release modes.

http://msdn.microsoft.com/en-us/library/ed8yd1ha.aspx

John Asbeck
Definitely. I have code in a project I'm working on that bypasses the login screen in Debug mode. Makes every test run that much quicker!
lc
+15  A: 

I see a lot of people replicate the functionality of Nullable<T>.GetValueOrDefault(T).

John Sheehan
In my opinion, ?? is cleaner in most cases, at least for primitive types (a ?? 0 instead of a.GetValueOrDefault()). The added burden of supplying the actual default value vanishes when you actually want another value.
OregonGhost
@OregonGhost: GetValueOrDefault (with or without a default value supplied) is about 33% faster than ??, although in truth, you won't notice a difference unless you do millions of iterations.
P Daddy
@P Daddy: I tried to benchmark myself: GetValueOrDefault seems only to be 33% faster in Debug builds. In Release builds both are about the same.
helium
+5  A: 

Has anybody used "props"?

You type "prop" and then press [TAB] twice, it generates useful code for your properties and can speed your typing.

I know this works in VS 2005 (I use it) but I don´t know in previous versions.

nmiranda
this is a feature of the IDE
BlackTigerX
I think it's VS 2005 and up. I use it a lot :-)
unforgiven3
Coderush shortens it to 'p' ;)
George Mauer
This is not a feature per se. It's a Visual Studio code snippet. You can create your own snippets, or even find some on internet. Check Dr WPF's code snippets, for example.
LBugnion
ctor can be used to create a constructor
Bhaskar
+12  A: 

How about the FlagsAttribute on an enumeration? It allows you to perform bitwise operations... took me forever to find out how to do bitwise operations in .NET nicely.

Martin Clarke
I think you can do bitwise operations on any enum, flags only affect the ToString() method
Olmo
This is actually pretty dangerous - it doesn't have any effect on the values assigned - it's purely a marker attribute to indicate your intention, so if you don't explicitly declare your enum values as powers of two then you will think you have a bitwise enum, but you won't really...
+7  A: 

I'm pretty sure everyone is familiar with operator overloading, but maybe some aren't.

class myClass
{
    private string myClassValue = "";

    public myClass(string myString)
    {
        myClassValue = myString;
    }

    public override string ToString()
    {
        return myClassValue;
    }

    public static myClass operator <<(myClass mc, int shiftLen)
    {
        string newString = "";
        for (int i = shiftLen; i < mc.myClassValue.Length; i++)
            newString += mc.myClassValue[i].ToString();
        mc.myClassValue = newString.ToString();
        return mc;
    }

    public static myClass operator >>(myClass mc, int shiftLen)
    {
        char[] newString = new char[shiftLen + mc.myClassValue.Length];

        for (int i = shiftLen; i < mc.myClassValue.Length; i++)
            newString[i] += mc.myClassValue[i - shiftLen];

        mc.myClassValue = new string(newString);
        return mc;
    }

    public static myClass operator +(myClass mc, string args)
    {
        if (args.Trim().Length > 1)
            mc.myClassValue += args;
        return mc;
    }

    public static myClass operator -(myClass mc, string args)
    {
        if (args.Trim().Length > 1)
        {
            Regex rgx = new Regex(args);
            mc.myClassValue = rgx.Replace(mc.myClassValue, "");
        }
        return mc;
    }
}

I think it's pretty cool to be able to shift a string left and right using << and >> or to remove a set of strings that follow a regular expression pattern using -=

myClass tmpClass = new myClass("  HelloWorld123");
tmpClass -= @"World";
tmpClass <<= 2;
Console.WriteLine(tmpClass);
SemiColon
As anyone who's worked with a C++ library that has lots of overloaded operators will tell you, overloaded operators are evil, evil, evil. Just write a method to do it.
endian
+1 I like the - operator on string..
chakrit
Great for math classes. Makes, for example, multiplying vector and matrices very to read, just aVector = anotherVector* aMatrix; instead of aVector=anotherVector.Multiply(aMatrix);
Sorskoot
Great for math classes, as @Sorskoot said, but that's about it. For pretty much any other class they're just really bad method names.
Danut Enachioiu
There are certain limited cases, in addition to mathematical classes, where certain operators make sense. I see nothing wrong with, say, myCollection += anItem; or if(someObject == anotherObject) {}.
Snarfblam
+3  A: 

One interesting thing I've learned is that different parts of the framework and C# language were written at different times, hence inconsistencies. For example, the framework itself violates many FxCop rules because the rules weren't all in place when the framework was written.

Also, the using statement was intended for delinieating "scopes" and not specifically for disposing resources. It was written after the lock statement. Eric Gunnerson once mentioned something along the lines of that if the using statement came first, they might have not needed to write the lock statement (though who knows, maybe they would have anyways), because the using statement might have been sufficient.

Haacked
+5  A: 

@lainMH,

Nullable booleans are useful when retrieving values from a database that are nullable and when putting values back in. Sometimes you want to know the field has not been set.

John Boker
+6  A: 

In reading the book on development of the .NET framework. A good piece of advice is not to use bool to turn stuff on or off, but rather use ENums.

With ENums you give yourself some expandability without having to rewrite any code to add a new feature to a function.

David Basarab
and besides they are more descriptive
Beatles1692
And they allow you to make your API harder to use incorrectly. Picture a method with a bunch of boolean parameters.
Dave Van den Eynde
+18  A: 

The C# ?? null coalescing operator -

Not really hidden, but rarely used. Probably because a lot of developers run a mile when they see the conditional ? operator, so they run two when they see this one. Used:

string mystring = foo ?? "foo was null"

rather than

string mystring;
if (foo==null)
    mystring = "foo was null";
else
    mystring = foo;
Frep D-Oronge
I understand your point, but the second example could be shortened without using the ?? operator. Just like string mystring = foo == null? "foo was null" : foo;Still long, I admit, but at least a bit shorter.
luiscubal
If the condition is a function that computes a result, with the ternary operator you would end up calling the function twice (in the case it evaluates to true). Whereas using ?? will only call it once.
DSO
+360  A: 

Read all the answers but I think lambdas and type inferrence is underrated.

I haven't seen anyone mention that Lambdas can have multiple statements and they double as a compatible delegate object automatically (just make sure the signature match) as in:

Console.CancelKeyPress +=
    (sender, e) => {
        Console.WriteLine("CTRL+C detected!\n");
        e.Cancel = true;
    };

Note that I don't have a new CancellationEventHandler nor do I have to specify types of sender and e, they're inferable from the event. Which is why this is less cumbersome to writing the whole delegate (blah blah) which also requires you to specify types of parameters.

Lambdas don't need to return anything and type inference is extremely powerful in context like this.

And BTW, you can always return Lambdas that make Lambdas in the functional programming sense. For example, here's a lambda that makes a lambda that handles a Button.Click event:

Func<int, int, EventHandler> makeHandler =
    (dx, dy) => (sender, e) => {
        var btn = (Button) sender;
        btn.Top += dy;
        btn.Left += dx;
    };

btnUp.Click += makeHandler(0, -1);
btnDown.Click += makeHandler(0, 1);
btnLeft.Click += makeHandler(-1, 0);
btnRight.Click += makeHandler(1, 0);

Note the chaining: (dx, dy) => (sender, e) =>

Now that's why I'm happy to have taken the functional programming class :-)

Other than the pointers in C, I think it's the other fundamental thing you should learn :-)

chakrit
That edit, it really should be a comment :)...
Vasco Fernandes
i don't get how that chain works. Can someone break it down?
goldenratio
The signature reads "Func<int, int, EventHandler>" so basically, it takes 2 integers and make an EventHandler that moves by the supplied numbers. EventHandler is itself a function, so by "chaining" I means that makeHandler is a function that makes a function... got it?
chakrit
That currying stuff is totally awesome. Lambdas are a great addition to C#.
Botz3000
I`m in love with those. :)
Arnis L.
That's awesome! Just the other day I was trying to figure out how to do that.
MighMoS
Functional paradigm FTW!
peSHIr
Very nice! @Botz3000 Just for the record, this is not currying, it's a higer order function. A function that returns a function. That;s even cooler ;-)
gjvdkamp
+4  A: 
Drakiula
+21  A: 

The #if DEBUG pre-processor directive. It is Useful for testing and debugging (though I usually prefer to go the unit testing route).

string customerName = null;
#if DEBUG
  customerName = "Bob"
#endif

It will only execute code block if Visual Studio is set to compile in 'Debug' mode. Otherwise the code block will be ignored by the compiler (and grayed out in Visual Studio).

Robert Durgin
Note that you can define any symbol and then use conditional compilation on that symbol. DEBUG just happens to be automatically defined for you by default.
xanadont
[Conditional("DEBUG")]-marked methods usually make for cleaner, easier to read code.
Danut Enachioiu
Good stuff. Saved me some trouble because when I publish on our network Drive D: is the needed path rather than Drive C: that I use on my local machine. Been burned in the past when I forgot to change it back!
Darcy
+12  A: 

I'm late to this party, so my first choices are already taken. But I didn't see anyone mention this gem yet:

Parallel Extensions to the .Net Framework

It has things like replace with Parallel.For or foreach with Parallel.ForEach

Joel Coehoorn
It is not really a language feature, though, as it is just a library which uses other language features cleverly.
Morten Christiansen
+15  A: 

true and false operators are really weird.

More comprehensive example can be found here.

Edit: There is related SO question What’s the false operator in C# good for?

Jakub Šturc
Been working with C# for 5 years and have never seen the true operator being overloaded. Makes sense since you can overload arithmetic and equality operators. Thanks!
Judah Himango
nice, never thought of that. Can be useful in some cases...
Sorskoot
+3  A: 

@Ed: FxCop will pull you up for that, you're casting twice.

Instead, you should be doing the following;

MyClass c = obj as MyClass;
if (obj != null)

The as will, if it cannot cast succesfully, assign a null.

+2  A: 

Returning IQueryable projections

protected void LdsPostings_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{   
    var dc = new MyDataContext();
    var query = dc.Posting.AsQueryable();

    if (isCondition1)
    {
        query = query.Where(q => q.PostedBy == Username);
        e.Result = QueryProjection(query);
        return;
    }

    ...

    if (isConditionN)
    {
        query = query.Where(q => q.Status.StatusName == "submitted");
        query = query.Where(q => q.ReviewedBy == Username);
        e.Result = QueryProjection(query);
        return;
    }
}

and rather than coding the projection multiple times, create a single method:

private IQueryable QueryProjection(IQueryable<Posting> query)
{
    return query.Select(p => new
    {
        p.PostingID,
        p.Category.CategoryName,
        p.Type.TypeName,
        p.Status.StatusName,
        p.Description,
        p.Updated,
        p.PostedBy,
        p.ReviewedBy,
    });
}
+16  A: 

I love using the @ character for SQL queries. It keeps the sql nice and formatted and without having to surround each line with a string delimiter.

string sql = @"SELECT firstname, lastname, email
               FROM users
               WHERE username = @username AND password = @password";
Nathan Lee
couldn't agree more
Greg Dean
I use '@' all the time, but I never thought of spanning multiple lines! :-P
bennage
One minor gripe with this (in this scenario) is that the spaces used for indenting and the newline characters end up in the string. Not generally a problem but one to be aware of.
BlackWasp
Another problem is that you're writing SQL in code :(
Matt Grande
I agree, its often not a good idea to write raw SQL like this, but its not always possible to get away from SQL code. Besides, it is just an example.
Nathan Lee
The SQL uses parameters. Apart from the SQL itself being hardcoded, I don't see any problems.
Dave Van den Eynde
+5  A: 

Thought about @dp AnonCast and decided to try it out a bit. Here's what I come up with that might be useful to some:

// using the concepts of dp's AnonCast
static Func<T> TypeCurry<T>(Func<object> f, T type)
{
    return () => (T)f();
}

And here's how it might be used:

static void Main(string[] args)
{

    var getRandomObjectX = TypeCurry(GetRandomObject,
        new { Name = default(string), Badges = default(int) });

    do {

        var obj = getRandomObjectX();

        Console.WriteLine("Name : {0} Badges : {1}",
            obj.Name,
            obj.Badges);

    } while (Console.ReadKey().Key != ConsoleKey.Escape);

}

static Random r = new Random();
static object GetRandomObject()
{
    return new {
        Name = Guid.NewGuid().ToString().Substring(0, 4),
        Badges = r.Next(0, 100)
    };
}
chakrit
Doesn't matter what it actually does, I would FIND a use for a method called TypeCurry! ;D
+45  A: 

I like looking up stuff in a list like:-

bool basketContainsFruit(string fruit) {
  return new[] { "apple", "orange", "banana", "pear" }.Contains(fruit);
}

Rather than:-

bool basketContainsFruit(string fruit) {
  return fruit == "apple" || fruit == "orange" || fruit == "banana" ||
    fruit == "pear";
}

Doesn't come up that much in practice, but the idea of making the items to match against the subject of the search can be really quite useful.

kronoz
very good. clean syntax.
frameworkninja
I'm curious what the compiler spits out for both implementations. The first one definitely looks cleaner and is a cute way of doing it. Could be slower though if the object is actually created in memory first then iterated through.
lc
you probably wanted fruit == banana?
ee
Kinda cool. But I would tweak it to use a static array so that it's not being newed every time the method is called.
xanadont
Used InList in Visual Foxpro. Created lame static method which accepts params list just to reproduce the same. I'm glad there is a cleaner approach. :)
Arnis L.
You know that the first method every call a new array is created and that calling Contains is lots of slower than a few comparisons. But yes, it is convenient :)
codymanix
@codymanix 0 yes, it's inefficient, but most of the time the inefficiency makes no difference to the perf of the application. Don't optimise unless the profiler says so :) - prefer readability over micro-perf issues unless the profiler disagrees :)
kronoz
But you can have the best of both worlds (at least for this example, or for any integral type) using switch. Example follows, but readability suffers in comments due to lack of newlines: `switch(fruit){ case "apple": case "orange": case "banana": case "pear": return true; default: return false; }`
P Daddy
except that in c# we don't have fallthrough
Fowl
@P Daddy - true, but does suffer from a lot of additional syntax. @Fowl - you can have fallthrough if the cases have no code (until the fallen-through case of course).
kronoz
@Fowl, yes we have, only for empty case statements, like this.
Michael
+32  A: 

A couple other attributes from the System.Diagnostics namespace are quite helpful.

DebuggerBrowsable will let you hide variables from the debugger window (we use it for all private backing variables of exposed properties). Along with that, DebuggerStepThrough makes the debugger step over that code, very useful for dumb properties (probably should be converted to auto-properties if you can take a dependency to the C# 3.0 compiler). As an example

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string nickName;
public string NickName    {
    [DebuggerStepThrough]
    get { return nickName; }
    [DebuggerStepThrough]
    set { this.nickName = value; }
}
bdukes
Beware! DebuggerStepThrough is very handy, but should only be used on trivial implementations. When you are debugging, methods marked with this attribute are skipped entirely by the debugger as if they aren't there (which hides the implementation details from you as you will single step right past it). Breakpoints inside the method won't ever be triggered.
Jason Williams
Hmm...neat trick but I think I'd take fields being visible in the debugger over having all that cruft in my class
George Mauer
Plus if this causes you to miss even one bug ever, it won't have been worth your time.
BlueRaja - Danny Pflughoeft
+11  A: 

ConditionalAttribute

Allows you to tell the compiler to omit the call to the method marked with the attribute under certain conditions (#define).

The fact that the method call is omitted also means that its parameters are not evaluated. This is very handy and it's what allows you to call expensive validation functions in Debug.Assert() and not worry about them slowing down your release build.

Andrew Burns
+80  A: 
Environment.NewLine

for system independent newlines.

nt
The annoying thing about this one, is that it isn't included into the compact framework.
Stormenet
Its worth pointing out that this is specific to the application's host platform - so if you are creating data intended for another system, you should use \n or \r\n appropriately.
Adrian
+19  A: 

Nesting Using Statements

Usually we do it like this:

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter()) {
    using (IndentedTextWriter itw = new IndentedTextWriter(sw)) {
        ... 
    }
}

But we can do it this way:

StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter())
using (IndentedTextWriter itw = new IndentedTextWriter(sw)) {
    ... 
}
John
Is this a feature specific to the using keyword? It just looks like the typical syntax where a statement (such as if, using, while) operates on either the next statement or statement block.Omitting the curly braces in these situations is not recommended in the code style guides I've read.
J c
Yeah I think its specific to the using keyword but good idea there. I did not think of that. :)
John
Its's not specific to using, you can write:if(Something) using(new Pen()) using(new Brush())for(;;)DoSometing();
Olmo
I agree...not a terribly good practice. I'm always very wary of omitting curly braces for maintainability purposes. For my own practices, if I have a single line statement to be used inside a block like that, I always turn it into one line, like if(bool) doStuff();
Adam Robinson
Love this! I'm always looking for ways to minimize indention spam.
xanadont
In similar manner i use curle-less ifs quite often.
Arnis L.
You can do this with every statement that can be nested.
Hermann
What's the difference?It's the same because you only have 1 block after the statement (same as the difference between:if (condition){if (condition2){}}and:if (condition)if (condition2)
Nissim
Tbh, imho, this is in absolutely no respect a "hidden" "feature" to non-beginner programmers. See Hermann's answer. I.e., then if(x)using(var y=...) could be considered a feature, too, as could "if(true)while(true)using(var x=...)switch(0){default:break}return;"
phresnel
+12  A: 

The extern alias keyword to reference two versions of assemblies that have the same fully-qualified type names.

Jakub Šturc
+2  A: 

PreviousPage property:

"The System.Web.UI.Page representing the page that transferred control to the current page."

It is very useful.

Eduardo Diaz
This is not a feature of C#, but rather of the System.Web.UI.Page class.
Jon
+4  A: 

TryParse method for each primitive type is great when validating user input.

double doubleValue
Double.TryParse(myDataRow("myColumn"), out doubleValue)
Michael Prewecki
I see I'm not the only one who forgets the "out" modifyer.
Jeffrey L Whitledge
doens't that return a bool? and set with an 'out' keyword?
Nicholas Mancuso
+31  A: 

Foreach uses Duck Typing

Paraphrasing, or shamelessly stealing from Krzysztof Cwalinas blog on this. More interesting trivia than anything.

For your object to support foreach, you don't have to implement IEnumerable. I.e. this is not a constraint and it isn't checked by the compiler. What's checked is that

  • Your object provide a public method GetEnumerator that
    • takes no parameters
    • return a type that has two members
      1. a parameterless method MoveNext that returns a boolean
      2. a property Current with a getter that returns an Object

For example,

class Foo
{
    public Bar GetEnumerator() { return new Bar(); }

    public struct Bar
    {
        public bool MoveNext()
        {
            return false;
        }

        public object Current
        {
            get { return null; }
        }
    }
}

// the following complies just fine:
Foo f = new Foo();
foreach (object o in f)
{
    Console.WriteLine("Krzysztof Cwalina's da man!");
}
Andreas H.R. Nilsson
+18  A: 

There are some really hidden keywords and features in C# related to the TypedReference undocumented class. The following keywords are undocumented:

  • __makeref
  • __reftype
  • __refvalue
  • __arglist

Examples of use:

// Create a typed reference
int i = 1;
TypedReference tr1 = __makeref(i);
// Get the type of a typed reference
Type t = __reftype(tr1);
// Get the value of a typed referece
int j = __refvalue(tr1, int); 
// Create a method that accepts and arbitrary number of typed references
void SomeMethod(__arglist) { ...
// Call the method
int x = 1;
string y = "Foo";
Object o = new Object();
SomeMethod(__arglist(x,y,o));
// And finally iterate over method parameters
void SomeMethod(__arglist) {
    ArgIterator ai = new ArgIterator(__arglist);
while(ai.GetRemainingCount() >0)
{
      TypedReference tr = ai.GetNextArg();
      Console.WriteLine(TypedReference.ToObject(tr));
}}
Pop Catalin
Why would you want to use these hidden undocumented keywords. They are likely hidden and undocumented for a reason, meaning they could change at any time. In my opinion this is risky.
Jon
Yes it is risky to use them, these hidden keywords, were introduced before generics to make interop,P/Invoke faster, because these features let you avoid boxing/unboxing value types.
Pop Catalin
But I'd also like to add that these keywords are used in BCL sources, so until MS rewrites the BCL (which won't happen till next version .Net CLR), they are rather safe use, if you're willing to do some rewriting posibly to run on CLR past 2.x.
Pop Catalin
Interesting, but what is it good for?
Qwertie
@Qwertie, it is for passing variable length list of arguments using the stack and not a params array which is heap allocated. (Basically for faster interop code, that doesn't allocate heap arrays). http://bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx also check this blog post
Pop Catalin
+2  A: 

@Robbie Rocketpants

"but my instincts tell me that this would cut a maximum of two type casts operations down to a maximum of one."

If you do the cast as you were suggesting in example 1 (using is & as), it results in 2 calls to the "is" operator. Because when you do "c = obj as MyClass", first it calls "is" behind the scenes, then if it fails that it simply returns null.

If you do the cast as you were suggesting in example 2,

c = (MyClass)obj

Then this actually performs the "is" operation again, then if it fails that check,it throws an exception (InvalidCastException).

So, if you wanted to do a lightweight dynamic cast, it's best to do the 3rd example you provided:

MyClass c;
if (obj is MyClass)
{
    c = obj as MyClass
}

if (c != null)
{
}

vs

MyClass c = obj as MyClass;

if (c != null)
{
}

You can see which is quicker, more consise and clearer.

Mark Ingram
+37  A: 

@David in Dakota:

Console.WriteLine( "-".PadRight( 21, '-' ) );

I used to do this, until I discovered that the String class has a constructor that allows you to do the same thing in a cleaner way:

new String('-',22);
Konamiman
Why not do this Console.WriteLine( "".PadRight( 22, '-' ) );
Jimmy
To achieve shorter and cleaner code, for example.
Konamiman
+52  A: 

InternalsVisibleTo attribute is one that is not that well known, but can come in increadibly handy in certain circumstances. It basically allows another assembly to be able to access "internal" elements of the defining assembly.

DAC
I use this regularly for writing unit tests against internal members of another assembly. That way unit tests can be excluded from deployments.
Drew Noakes
Cool... my mate had problem recently with tests.
Arnis L.
+1: Very nice. I was looking for something akin to C++'s 'friend' for a long time and never came across this (like Drew noted, for writing unit tests of internal elements)
SnOrfus
This is definitely my best discovery from this topic. The best solution I had found on my own to give unit tests in a different assembly access to internal methods was to make the methods protected and write pseudo-mock classes in my test assembly that inherited from the class being tested.
Mark Nelson
+31  A: 

I just found out about this one today -- and I've been working with C# for 5 years!

It's the namespace alias qualifier:

extern alias YourAliasHere;

You can use it to load multiple versions of the same type. This can be useful in maintenance or upgrade scenarios where you have an updated version of your type that won't work in some old code, but you need to upgrade it to the new version. Slap on a namespace alias qualifier, and the compiler will let you have both types in your code.

Judah Himango
example scenario?
chakrit
See http://msdn.microsoft.com/en-us/library/ms173212(VS.80).aspx
Judah Himango
+14  A: 

I like the keyword continue.

If you hit a condition in a loop and don't want to do anything but advance the loop just stick in "continue;".

E.g.:

foreach(object o in ACollection)
{
  if(NotInterested)
     continue;
}
nialljsmith
-1: Use of continue's, break's etc. are one step away from goto's (which are evil). They make the flow of execution difficult to follow in complex programs and will eventually lead you to writing spaghetty code.
Jon Cage
+1 to offset Jon Cage. If continue/break are evil, then so is return. continue/break can be used to terminate a loop early (continue terminates just the current iteration, break terminates the entire loop), just as return can be used to terminate a function early. And early out can be much better than deeply-nested ifs. And goto is not evil, just not often necessary. It got a bad rep from the "spaghetti code" often created in older languages lacking better constructs. Having these better constructs leads to cleaner code and much less need for goto, but not none. Use the right tool for the job.
P Daddy
+1 to doubly offset Jon Cage. Partially because he can't spell _spaghetti_. Using continue, break, and goto are perfectly valid means to an end. If you're using them excessively, you're probably doing something wrong, but code does call for it at times. If a developer has a hard time following that, they should probably look for a new profession as gotos are at the ancestral roots of a lot of modern programming, if anyone remembers BASIC.
blesh
+1 to triple the offset: while I might use if(!NotInterested){...} in the example, I would not drag break into this. break is required by switch-case, and to escape out of a loop when a condition is met. return is just as important to stopping a function from continuing as is break within an interation.
dboarman
@blesh I remember basic... on my old Acorn A3000. ;D
+8  A: 

Not a C# specific thing, but I am a ternary operations junkie.

Instead of

if (boolean Condition)
{
    //Do Function
}
else
{
    //Do something else
}

you can use a succinct

booleanCondtion ? true operation : false operation;

e.g.

Instead of

int value = param;
if (doubleValue)
{
    value *= 2;
}
else
{
    value *= 3;
}

you can type

int value = param * (tripleValue ? 3 : 2);

It does help write succinct code, but nesting the damn things can be nasty, and they can be used for evil, but I love the little suckers nonetheless

johnc
For the sake of correctness, this pattern can only be used in situations like:if(condition) a = //some valueelse a = // other value
Olmo
It's great for conditional flags too -- flags = (shouldRun ? RUN : 0) | (shouldHide ? HIDE : 0);
lc
I think making easy to maintain code is more important than making code that fits nicely on a line. Also, if someone wants to do this:else { // Do something else // and this other thing}Suddenly your single line ternary has to be converted to curly braces.
jcollum
+2  A: 

Saw a mention of List.ForEach above; 2.0 introduced a bevy of predicate-based collection operations - Find, FindAll, Exists, etc. Coupled with anonymous delegates you can almost achieve the simplicity of 3.5's lambda expressions.

Thomas H
+33  A: 

A couple of things I like:

-If you create an interface similar to

 public interface SomeObject<T> where T : SomeObject<T>, new()

you force anything that inherits from this interface to contain a parameterless constructor. It is very useful for a couple of things I've run across.

-Using anonymous types to create a useful object on the fly:

var myAwesomeObject = new {Name="Foo", Size=10};

-Finally, many Java developers are familiar with syntax like:

public synchronized void MySynchronizedMethod(){}

However, in C# this is not valid syntax. The workaround is a method header:

 [MethodImpl(MethodImplOptions.Synchronized)]
 public void MySynchronizedMethod(){}
Bryan
These are all good ideas. This site generally prefers one idea per answer so they can be rated individually. I would have given you three ratings :)
Drew Noakes
[MethodImpl(MethodImplOptions.Synchronized)] = lock(this) = bad
Greg Dean
I'm not familiar with "Sychronized" method, can you tell me what they do?
Matt Grande
"you force anything that inherits from this interface to contain a parameterless constructor"Strictly speaking, no you don't - you force any class that implements your interface to prove that it know the name of a class that implements the interface and has a parameterless constructor. That's not the same thing.class A : SomeObject<A> { public A() // required }class B : SomeObject<A> { } // will compile fine, no constructor.
James Hart
Man, that public-constructor constraint is twisted. I like it.
Joe White
@Matt, in this case, synchronised (or the methodimpl attribute) locks on the current object while the method is processing. However, the attribute causes a lock(this) while it does it: and in CLR via C# I recall that this was not a good idea (iirc, it exposed a potential security vulnerability but the book is all the way on the other side of the house, and it's really late). This is why most people will lock on a private object member variable instead of lock(this).
SnOrfus
+144  A: 

I think one of the most under-appreciated and lesser-known features of C# (.NET 3.5) are Expression Trees, especially when combined with Generics and Lambdas. This is an approach to API creation that newer libraries like NInject and Moq are using.

For example, let's say that I want to register a method with an API and that API needs to get the method name

Given this class:

public class MyClass
{
     public void SomeMethod() { /* Do Something */ }
}

Before, it was very common to see developers do this with strings and types (or something else largely string-based):

RegisterMethod(typeof(MyClass), "SomeMethod");

Well, that sucks because of the lack of strong-typing. What if I rename "SomeMethod"? Now, in 3.5 however, I can do this in a strongly-typed fashion:

RegisterMethod<MyClass>(cl => cl.SomeMethod());

In which the RegisterMethod class uses Expression<Action<T>> like this:

void RegisterMethod<T>(Expression<Action<T>> action) where T : class
{
    var expression = (action.Body as MethodCallExpression);

    if (expression != null)
    {
        // TODO: Register method
        Console.WriteLine(expression.Method.Name);
    }
}

This is one big reason that I'm in love with Lambdas and Expression Trees right now.

jolson
I have a reflection utility class that does the same with FieldInfo, PropertyInfo, etc...
Olmo
Yes, this is powerful.
Charlie Flowers
I am really loving expressions as well... Good post
J.13.L
Awesome! I agree Expression Trees are underrated... Just hope Jon Skeet covers this in his new edition of C# in depth ;)
Vyas Bharghava
C# 3.5 ?????????????
harryovers
+255  A: 

My favorite trick is using the null coalesce operator and parentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }
Will
Now that is a cool technique: in-lining the assignment when null!
Andrew Robinson
nice, I will probably use this pattern when I can!
p.campbell
Oh this is cool! I use that idiom all the time in Ruby (everyone does), but had not yet realized that it now maps directly to c#!!
Charlie Flowers
This is a great technique. I will use this for sure!
goldenratio
That is great! I knew this operator but i never thought about using it that way until now. 2 lines less per lazy-init property :D
Botz3000
Don't you find it hard to read?
Riri
Its slightly hard to read for the noo... er, inexperienced. But its compact and contains a couple patterns and language features that the programmer should know and understand. So, while it is hard at first, it provides the benefit of being a reason to learn.
Will
love it!!!!!!!!!!!!!this is really cool!!!
Shimmy
Hah. Nice one i like it.
WOPR
Lazy instantiation is somewhat malpractice because it's a poor mans choice to avoid thinking about class invariants. It also has concurrency issues.
John Leidegren
I usually use it as .. return _foo = _foo ?? new List<Foo>(); ... IMO a little easier to read.
chakrit
Sorry, misread what you wrote @chakrit... Its not bad, but it doooes take about 2 ms more to execute yours...
Will
I've always been informed this is bad practise, that calling a property should not do something as such. If you had a set on there and set the value to null, it would be very strange for someone using your API.
Ian
Its nice, but way harder to read.
George
@Ian: I suppose if I tracked down and beat up anybody using my API that would also be very strange to anyone using it. Perhaps I'll strike that from my "foolish things that anybody with sense wouldn't do but seem like a pretty cool idea to me" list. I just can't fathom what your comment has to do with anything. It boggles my mind. "If you did something stupid it would be stupid." I suppose it makes sense, but why would I? I'm so confused.
Will
@Will: what I think Ian is referring too is this: calling `ListOfFoo` influences the internal state of your class. User A of your API, who calls `ListOfFoo` before `FunctionThatDoesSomethingWithFoo`, might see a different behaviour than User B, who calls `FunctionThatDoesSomethingWithFoo` without calling `ListOfFoo` first.
jan
@jan I'm not sure how that is. The behavior outside of the body of the 'get' is the same. Are you referring to the practice of bypassing the public property when accessing it within the class? I consider that bad practice, personally. Still, Ian says that IF I had a setter it would be bad; yes, and if I hit my head with a hammer it would be bad also, that's why I don't. So I'm still confused about his concerns. BRB, not jumping off a cliff naked.
Will
+1 for this. However its worth looking at the revision history of this answer. There is a good point about being careful with the syntax of this so you get the object instance back you think you will. check it out
Matt Joslin
That is the coolest way I've ever seen to do a Singleton pattern...Awesome.
Stargazer712
I think Ian's point about the setter is well made: if I am using the class and use a setter to set ListOfFoo to null, the fact that it would be non-null the next time I call it is breaking expectations of how a property should behave. So it is correct to point out that this pattern should not be used with a setter.
Joel in Gö
@Joel except that setting ListOfFoo to null is neither part of the class contract nor is it good practice. That's why there isn't a setter. Its also why ListOfFoo is guaranteed to return a collection and never a null. Its just a very odd complaint that if you do two bad things (make a setter and set a collection to null) this will result in your expectations being wrong. I wouldn't suggest doing Environment.Exit() in the getter, either. But that also has nothing to do with this answer.
Will
@Will I don't think this is hard to read at all, and I'm not even a C# programmer. However, I still think this is not really good practice for a property. What's the gain of the lazy initialization? Not much. Why complicate the state space of the object for no good reason? The feature itself is neat (although I'm not sure how the evaluation of an expression is considered a **hidden feature**...), but I think this particular application is a bit of a bad idea.
TM
@Charlie Flowers, putting initializer code in a getter is a Ruby standard practice (Ruby idiom)?!
James
@James, I can't tell if you're asking me or telling me. But yes, it is a standard idiom in Ruby. Particularly for checking if the value is null because it has not been initialized yet. So it's lazy initialization, and probably more importantly it minimizes the amount of boilerplate needed to define a property and get it working.
Charlie Flowers
Another vote for not allowing API users to reassign collections in most circumstances. Also wanted to agree that you should be using your own properties in your class methods, not accessing the private fields directly. Still, I generally prefer to ensure the existence of a collection with `readonly` initialization, unless the list _needs_ to be lazy-loaded.
WCWedin
+32  A: 

The volatile keyword to tell the compiler that a field can be modified by multiple threads concurrently.

Jakub Šturc
+37  A: 

I couldn't see this looking above - one that I didn't realise you could do until recently is to call one constructor from another:

class Example
{
    public Example(int value1)
        : this(value1, "Default Value")
    {
    }

    public Example(int value1, string value2)
    {
        m_Value1 = value1;
        m_value2 = value2;
    }

    int m_Value1;
    string m_value2;
}
Groky
Although I used this thing cross classes, I never thought of using it in the same class.I have been looking all over the place for something like that! Great!
borisCallens
That's great. I always used that calling the base, but never knew you could use it in the same class!
lc
Damn... I was looking for this long time ago.
Arnis L.
can you guys link to somewhere showing a usage example of this for cross class and base class?
Maslow
Yea, I use this all of the time. It really is handy, and very intuitive I think.
SnOrfus
I think you meant: public Example(int value1) : this(*value1*, "Default Value") { }
rball
Well spotted rball! 1 year, 21 upvotes and nobody spotted my deliberate mistake ;) Fixed.
Groky
Thanks for this one.
Braveyard
Thanks to C# 4.0 there's no reason to do something this simple, you can just make the second parameter optional (please excuse the lack of line breaks):public Example(int value1, string value2 = "Default Value") {...}
Corey Ogburn
+7  A: 
  1. I can't comment yet, but note that by default VS2008 automatically steps over properties, so the DebuggerStepThrough attribute is no longer needed in that case.

  2. Also, I haven't noticed anyone showing how to declare a parameter-less lambda (usefull for implementing Action<>)

    () => DoSomething(x);

    You should also read up on closures - I'm not clever enough to explain them properly. But basically it means that the compiler does clever stuff so that the x in that line of code will still work even if it goes 'out of scope' after creating the lambda.

  3. I also discovered recently that you can pretend to ignore a lambda parameter:

    (e, _) => DoSomething(e)

    It's not really ignoring it, it's just that _ is a valid identifier. So you couldn't ignore both of the parameters like that, but I think it is a kind of neat way to indicate that we don't care about that parameter (typically the EventArgs which is .Empty).

Benjol
+5  A: 

System.Runtime.Remoting.Proxies.RealProxy

It enables Aspect Oriented Programming in C#, and you can also do a lot of other fancy stuff with it.

Thomas Danecker
How do you get AOP from something that "supports remoting infastructure"?
Qwertie
Because it supports the remoting infrastructure by enabling AOP ;) Look here for the technical aspects of it: http://tdanecker.blogspot.com/2007/09/interception-with-proxies.html
Thomas Danecker
+244  A: 

Aliased generics:

using ASimpleName = Dictionary<string, Dictionary<string, List<string>>>;

It allows you to use ASimpleName, instead of Dictionary<string, Dictionary<string, List<string>>>.

Use it when you would use the same generic big long complex thing in a lot of places.

BlackTigerX
Haha that's kind of cool, but I'm very glad that C# 3 has implemented the "var" keyword which has got rid of some of the ugliness with generic instantiation like: Dictionary<string, Dictionary<string, List<string>>> myDic = new Dictionary<string, Dictionary<string, List<string>>>();
cbp
+1 This was new to me. I used to subclass to death here.
Dave Van den Eynde
Holy .... this one truly made reading this post worthy. 8)
Arnis L.
If you came from C/C++ ... you'll definitely miss the typedefs operator thus making you look for this kinda feature in the first place...
chakrit
I find myself wishing for a better typedef-like feature. e.g., "using StringIndexedHashtable<T> = Dictionary<string, T>" Or even just "using Dictionary = System.Collections.Generic.Dictionary" would be helpful in some cases, even if it had to be written "using Dictionary<TKey, TValue> = System.Collections.Generic.Dictionary<TKey, TValue>"
Dathan
+1 Wow, did not know about this.
Kyle
+5  A: 

new modifier

Usage of the "new" modifier in C# is not exactly hidden but it's not often seen. The new modifier comes in handy when you need to "hide" base class members and not always override them. This means when you cast the derived class as the base class then the "hidden" method becomes visible and is called instead of the same method in the derived class.

It is easier to see in code:

public class BaseFoo
{
    virtual public void DoSomething()
    {
        Console.WriteLine("Foo");
    }
}

public class DerivedFoo : BaseFoo
{
    public new void DoSomething()
    {
        Console.WriteLine("Bar");
    }
}

public class DerivedBar : BaseFoo
{
    public override void DoSomething()
    {
        Console.WriteLine("FooBar");
    }
}

class Program
{
    static void Main(string[] args)
    {
        BaseFoo derivedBarAsBaseFoo = new DerivedBar();
        BaseFoo derivedFooAsBaseFoo = new DerivedFoo();

        DerivedFoo derivedFoo = new DerivedFoo();

        derivedFooAsBaseFoo.DoSomething(); //Prints "Foo" when you might expect "Bar"
        derivedBarAsBaseFoo.DoSomething(); //Prints "FooBar"

        derivedFoo.DoSomething(); //Prints "Bar"
    }
}

[Ed: Do I get extra points for puns? Sorry, couldn't be helped.]

Sixto Saez
//Prints "Foo" when you might expect "Bar"I think that comment says it all. The program no longer operates as expected. Member hiding in this way is unwise. It does not have the semantics of overriding. See: http://blog.colinmackay.net/archive/2008/10/10/4264.aspx
Colin Mackay
+31  A: 

Don't forget about goto.

John Spurlock
No, lets forget it. ;)
Gary Willoughby
No, lets abuse it till kingdom come. ;)
chakrit
Voted up for irony.
BlackWasp
Yeah, don't forget it--make sure to nuke it when you have a chance!
Loren Pechtel
from the old days of AppleSoft basic: gosub/return ;)
dboarman
I want this answer to reach the top.
Dan Tao
This should have remained hidden.
Ray Vega
I once had a co-worker that claimed that he had found a legitimate use of goto, that is as use that made function clearer that with other alternatives. I looked at the code and asked some questions how it worked. When I asked him at the coffe break one hour later he had changed to a break (I think). At least goto is very general and easy to implement in a compiler.
Gorgen
+10  A: 

Falling through switch-cases can be achieved by having no code in a case (see case 0), or using the special goto case (see case 1) or goto default (see case 2) forms:

switch (/*...*/) {
    case 0: // shares the exact same code as case 1
    case 1:
        // do something
        goto case 2;
    case 2:
        // do something else
        goto default;
    default:
        // do something entirely different
        break;
}
Alex Lyman
Note that "goto case" is discouraged in this article: http://msdn.microsoft.com/en-us/vcsharp/aa336815.aspx
J c
I'd be one to suggest that _any_ use of 'goto' is discouraged in general -- but that doesn't mean that it isn't useful.
Alex Lyman
I think in a switch is only place a goto _is_ acceptable.
Matt Grande
I think I would be very upset if I encountered code like this and had to debug or maintain it...
Richard Ev
@Richard E: I think that's why its well-hidden, really. Only power users really read ECMA-334, so its rather likely that its only used sparingly.
Alex Lyman
case 2:default: break;will return the same thing even without the goto default;
Martin Ongtangco
@Martin Ongtangco: Only if case 2's "// do something else" is empty.
Alex Lyman
+4  A: 

The #region {string} and #endregion pair is very neat for grouping code (outlining).

#region Using statements
using System;
using System.IO;
using ....;
using ....;
#endregion

The code block can be compressed to a single describing line of text. Works inside functions aswell.

Statement
I suppose it's a matter of preference, but when I'm looking at source code I'd rather not have to be expanding hidden sections of code. Regions used sparingly (outside of methods) aren't an issue, but beware using them in lieu of writing readable/maintainable code in the first place.
J c
I quite like regions but some people overuse them. I couldn't agree with you more about keeping them out of methods. I have seen the whole purpose of a method obfuscated by a region that was collapsed by default but that a colleague had accidentally hidden a key variable change within.
BlackWasp
I agree with not using regions inside methods.By the way, Ctrl-M, Ctrl-L will expand all the regions in Visual Studio
LBugnion
Basically, whenever I feel a need for regions inside a method, that region is often better suited as a method call of its own instead.
Statement
Some forms have a lot of controls, with a lot of controls comes a lot of event handlers, seems like a good place to use regions. Event code vs. event called or non-event code.
Maslow
But isn't a #region around the using directives redundant? They're still collapsible without it.
TheBeardyMan
People are still using regions? Regions are a code smell: http://morten.lyhr.dk/2008/07/visual-studio-regions-are-code-smell.html
Chuck Conway
+28  A: 

Static constructors.

Instances:

public class Example
{
    static Example()
    {
        // Code to execute during type initialization
    }

    public Example()
    {
        // Code to execute during object initialization
    }
}

Static classes:

public static class Example
{
    static Example()
    {
        // Code to execute during type initialization
    }
}

MSDN says:

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

For example:

public class MyWebService
{
    public static DateTime StartTime;

    static MyWebService()
    {
        MyWebService.StartTime = DateTime.Now;
    }

    public TimeSpan Uptime
    {
        get { return DateTime.Now - MyWebService.StartTime; }
    }
}

But, you could also just as easily have done:

public class MyWebService
{
    public static DateTime StartTime = DateTime.Now;

    public TimeSpan Uptime
    {
        get { return DateTime.Now - MyWebService.StartTime; }
    }
}

So you'll be hard-pressed to find any instance when you actually need to use a static constructor.

MSDN offers useful notes on static constructors:

  • A static constructor does not take access modifiers or have parameters.

  • A static constructor is called automatically to initialize the class before the first instance is created
    or any static members are referenced.

  • A static constructor cannot be called directly.

  • The user has no control on when the static constructor is executed in the program.

  • A typical use of static constructors is when the class is using a log file and the constructor is used to write
    entries to this file.

  • Static constructors are also useful when creating wrapper classes for
    unmanaged code, when the constructor
    can call the LoadLibrary method.

  • If a static constructor throws an exception, the runtime will not
    invoke it a second time, and the type will remain uninitialized for the
    lifetime of the application domain in which your program is running.

The most important note is that if an error occurs in the static constructor, a TypeIntializationException is thrown and you cannot drill down to the offending line of code. Instead, you have to examine the TypeInitializationException's InnerException member, which is the specific cause.

Chris
No need to lock as all the static members are thread safe.
nils_gate
Actually there was an internal web service that also could be used to change the value of connectionString too. So the lock was needed, no? I didn't remove the lock because I just copied the code and then added the comment.
Chris
Okay, I completely removed the example and made everything more clear (I hope!).
Chris
@nils_gate: No, that's a nasty mistake. You probably think this because MSDN docs often say "static members of SoAndSo class are thread safe." This is not because they're *intrinsically* thread safe. Quite the opposite, in fact. Since any thread can access static members without even having to share an object reference, then static members are more likely to find themselves in a race condition, which is why Microsoft has taken the trouble to *make* most of their static members thread safe.
P Daddy
)
chapluck
+2  A: 

Some concurrency utilities in the BCL might qualify as hidden features.

Things like System.Threading.Monitor are used internally by the lock keyword; clearly in C# the lock keyword is preferrable, but sometimes it pays to know how things are done at a lower level; I had to lock in C++/CLI, so I encased a block of code with calls to Monitor.Enter() and Monitor.Exit().

unforgiven3
+6  A: 

Literals can be used as variables of that type. eg.

Console.WriteLine(5.ToString());
Console.WriteLine(5M.GetType());   // Returns "System.Decimal"
Console.WriteLine("This is a string!!!".Replace("!!", "!"));

Just a bit of trivia...

There's quite a few things people haven't mentioned, but they have mostly to do with unsafe constructs. Here's one that can be used by "regular" code though:

The checked/unchecked keywords:

public static int UncheckedAddition(int a, int b)
{
    unchecked { return a + b; }
}

public static int CheckedAddition(int a, int b)
{
    checked { return a + b; } // or "return checked(a + b)";
}

public static void Main() 
{
    Console.WriteLine("Unchecked: " + UncheckedAddition(Int32.MaxValue, + 1));  // "Wraps around"
    Console.WriteLine("Checked: " + CheckedAddition(Int32.MaxValue, + 1));  // Throws an Overflow exception
    Console.ReadLine();
}
ilitirit
+2  A: 

Before lambda comes into play, it's anonymous delegate. That could be used for blanket code similar to Ruby's blockgiven. I haven't tested how lambda works though because I want to stick with .NET 2.0 so far.

For example when you want to make sure you remember to close your HTML tags:

MyHtmlWriter writer=new MyHtmlWriter();
writer.writeTag("html", 
  delegate ()
  { 
    writer.writeTag("head", 
       delegate() 
       { 
           writer.writeTag("title"...);
       }
    )
  })

I am sure if lambda is an option, that could yield much cleaner code :)

goodwill
if you're using VS2008, then use the lambda syntax. It's syntactic sugar not a framework feature. It gets compiled to an anonymous delegate anyway and is just less noisy to read.
Hamish Smith
+1  A: 

If 3rd-party extensions are allowed, then C5 and Microsoft CCR (see this blog post for a quick introduction) are a must-know.

C5 complements .Net's somewhat lacking collections library (not Set???), and CCR makes concurrent programming easier (I hear it's due to be merged with Parallel Extensions).

ripper234
+45  A: 

Dictionary.TryGetValue(K key, out V value)

Works as a check and a get in one. Rather than;

if(dictionary.ContainsKey(key)) 
{
    value = dictionary[key];
    ...
}

you can just do;

if(dictionary.TryGetValue(key, out value)) 
{ ... }

and the value has been set.

Wyzfen
Another benefit of TryGetValue is that if your dictionary is synchronized, there is no race condition. Compared to ContainsKey where another thread could remove the item you are looking for between calls.
Guvante
TryGetValue throws if the key is null -- so much for avoiding axceptions. I use a TryGetValue2() extension method to get around this problem.
Qwertie
+4  A: 

I think a lot of people know about pointers in C but are not sure if it works in C#. You can use pointers in C# in an unsafe context:

static void Main()
{
    int i;
    unsafe
    {               
        // pointer pi has the address of variable i
        int* pi = &i; 
        // pointer ppi has the address of variable pi
        int** ppi = &pi;
        // ppi(addess of pi) -> pi(addess of i) -> i(0)
        i = 0;
        // dereference the pi, i.e. *pi is i
        Console.WriteLine("i = {0}", *pi); // output: i = 0
        // since *pi is i, equivalent to i++
        (*pi)++;
        Console.WriteLine("i = {0}", *pi); // output: i = 1
        // since *ppi is pi, one more dereference  *pi is i 
        // equivalent to i += 2
        **ppi += 2;
        Console.WriteLine("i = {0}", *pi);// output: i = 3
    }
    Console.ReadLine();
}
codemeit
+1  A: 

Some ?? weirdness :)

Delegate target =
  (target0 = target as CallTargetWithContext0) ??
  (target1 = target as CallTargetWithContext1) ??
  (target2 = target as CallTargetWithContext2) ??
  (target3 = target as CallTargetWithContext3) ??
  (target4 = target as CallTargetWithContext4) ??
  (target5 = target as CallTargetWithContext5) ??
  ((Delegate)(targetN = target as CallTargetWithContextN));

Interesting to note the last cast that is needed for some reason. Bug or by design?

leppie
+8  A: 
System.Diagnostics.Debug.Assert (false);

will trigger a popup and allow you to attach a debugger to a running .NET process during execution. Very useful for those times when for some reason you can't directly debug an ASP.NET application.

axel_c
So will Debugger.Break();
P Daddy
don't these result in some pretty unclear message boxes being display to a user if one of these happens in production?
Maslow
Well, yes, that's why you should #ifdef them out and/or remove them from production builds.
axel_c
@Maslow, no - the Debug.Assert method is flagged with [Conditional("DEBUG")], which means calls to it get removed in non-DEBUG builds. Unless you build your production code with the DEBUG flag, in which case...
Danut Enachioiu
use System.Diagnostics.Debugger.Launch()
Nissim
+9  A: 

There are operators for performing implicit and explicit user-defined type conversion between the declared class and one or more arbitrary classes. The implicit operator effectively allows the simulation of overloading the assignement operator, which is possible in languages such as C++ but not C#.

It doesn't seem to be a feature one comes across very often, but it is in fact used in the LINQ to XML (System.Xml.Linq) library, where you can implicitly convert strings to XName objects. Example:

XName tagName = "x:Name";

I discovered this feature in this article about how to simulate multiple inheritance in C#.

Flory
+125  A: 

Unions (the C++ shared memory kind) in pure, safe C#

Without resorting to unsafe mode and pointers, you can have class members share memory space in a class/struct. Given the following class:

[StructLayout(LayoutKind.Explicit)]
public class A
{
    [FieldOffset(0)]
    public byte One;

    [FieldOffset(1)]
    public byte Two;

    [FieldOffset(2)]
    public byte Three;

    [FieldOffset(3)]
    public byte Four;

    [FieldOffset(0)]
    public int Int32;
}

You can modify the values of the byte fields by manipulating the Int32 field and vice-versa. For example, this program:

    static void Main(string[] args)
    {
        A a = new A { Int32 = int.MaxValue };

        Console.WriteLine(a.Int32);
        Console.WriteLine("{0:X} {1:X} {2:X} {3:X}", a.One, a.Two, a.Three, a.Four);

        a.Four = 0;
        a.Three = 0;
        Console.WriteLine(a.Int32);
    }

Outputs this:

2147483647
FF FF FF 7F
65535

just add using System.Runtime.InteropServices;

ZeroBugBounce
+1 awesome! This would enables a whole lots of number manipulation tricks from C++ days :-)
chakrit
Cool, but I totally would murder someone who I saw doing this.
George Mauer
@George +1 :D .
Arnis L.
@George, works wonders when you are communicating with legacy apps over sockets using c union declarations.
scottm
There's also meaningful to say int and float at offset 0. It's what you need if you want to manipulate floating point numbers as bit masks, which you sometimes want to. Especially if you wanna learn new things about floating point numbers.
John Leidegren
The annoying thing about this is if you're going to use this is a struct the compiler will force you to set ALL the variables in the init function. So if you have: public A(int int32) { Int32 = int32; } it will throw "Field 'One' must be fully assigned before control is returned to the caller", so you have to set One = Two = Three = Four = 0; as well.
manixrock
This has its uses, mostly with binary data. I use a "Pixel" struct with an int32 @ 0, and four bytes for the four components @ 0, 1, 2, and 3. Great for manipulating image data quickly and easily.
Snarfblam
i have to work with variant unions in c on vms :( - 'orrible stuff
Matt Joslin
**Warning: This approach does not take into account endianness.** That means your C# code will not run the same way on all machines. On little-endian CPUs (which store the least significant byte first), the behavior shown will be used. But on big-endian CPUs, the bytes will come out reversed from what you expected. Beware how you use this in production code - your code may not be portable to certain mobile devices and other hardware, and may break in non-obvious ways (eg two files ostensibly in the same format but actually with byte order reversed).
Ray Burns
This approach is not uniform, for example if you try to repeat the same trick with fields of array type, i.e interpret uint[] as int[] - result shall be unverifyable.
desco
+40  A: 

A few hidden features I've come across:

  • stackalloc which lets you allocate arrays on the stack
  • Anonymous methods with no explicit parameter list, which are implicitly convertible to any delegate type with non-out/ref parameters (very handy for events, as noted in an earlier comment)
  • A lot of people aren't aware of what events really are (an add/remove pair of methods, like get/set for properties); field-like events in C# really declare both a variable and an event
  • The == and != operators can be overloaded to return types other than bool. Strange but true.
  • The query expression translation in C# 3 is really "simple" in some ways - which means you can get it to do some very odd things.
  • Nullable types have special boxing behaviour: a null value gets boxed to a null reference, and you can unbox from null to the nullable type too.
Jon Skeet
Thanks Jon, this is pretty crazy list :)
Joan Venge
wouldn't mind a little elaboration on this post1
Alex Baranosky
@GordonG: Which bit of it?
Jon Skeet
Unfortunately, `stackalloc` requires unsafe context.
RickNZ
+9  A: 

One feature that I only learned about here on Stack Overflow was the ability to set an attribute on the return parameter.

[AttributeUsage( AttributeTargets.ReturnValue )]
public class CuriosityAttribute:Attribute
{
}

public class Bar
{
    [return: Curiosity]
    public Bar ReturnANewBar()
    {
        return new Bar();
    }
}

This was truly a hidden feature for me :-)

Mihai Lazar
What does it do?
It applies Curiosity as an attribute to the returned instance.
Mihai Lazar
That's cool stuff, all though it's usefulness escapes me for the time being.
Skurmedel
You can also use attributes on method parameters (common in MVC) eg. public ActionResult Register([Bind(Prefix = "Person")] string name)
Dan Diplo
Very interesting - thanks.
Steve Dunn
+8  A: 

String interning. This is one that I haven't seen come up in this discussion yet. It's a little obscure, but in certain conditions it can be useful.

The CLR keeps a table of references to literal strings (and programmatically interned strings). If you use the same string in several places in your code it will be stored once in the table. This can ease the amount of memory required for allocating strings.

You can test if a string is interned by using String.IsInterned(string) and you can intern a string using String.Intern(string).

Note: The CLR can hold a reference to an interned string after application or even AppDomain end. See the MSDN documentation for details.

dariom
never heard of that...
George Mauer
Interned strings are held in small pages on the Large Object Heap. If your set of strings is unbounded, you will fragment the LOH with small, long-lived string intern pages.
Paul Ruane
+31  A: 

The params keyword, i.e.

public void DoSomething(params string[] theStrings)
{
  foreach(string s in theStrings)
  {
    // Something with the Strings…
  }
}

Called like

DoSomething(“The”, “cat”, “sat”, “on”, “the” ,”mat”);
Tim Jarvis
Wow I didn't know about this one and I've been developing in .NET for about 2 years now.
Darcy
+13  A: 

Labeling my endregions...

#region stuff1
 #region stuff1a
 //...
 #endregion stuff1a
#endregion stuff1
tsilb
I like to label other people's end regions
Maslow
@Maslow: lmao...
dboarman
+39  A: 

I picked this one up when using ReSharper:

Implicit Method Group Conversion

//If given this:
var myStrings = new List<string>(){"abc","def","xyz"};
//Then this:
myStrings.ForEach(s => Console.WriteLine(s));
//Is equivalent to this:
myStrings.ForEach(Console.WriteLine);

See "Implicit Method Group Conversion in C#" for more.

+1 I picked it up due to Resharper too.
chakrit
not for Debug.WriteLine though cuz it uses a #if DEBUG statement internally.
Henrik
Nice! Very obscure, had no idea you could do this.
John
No idea this was possible, I was always using foreach(). Cheers!
AndyC
+5  A: 

Instead of using int.TryParse() or Convert.ToInt32(), I like having a static integer parsing function that returns null when it can't parse. Then I can use ?? and the ternary operator together to more clearly ensure my declaration and initialization are all done on one line in a easy-to-understand way.

public static class Parser {
    public static int? ParseInt(string s) {
        int result;
        bool parsed = int.TryParse(s, out result);
        if (parsed) return result;
        else return null;
    }
    // ...
}

This is also good to avoid duplicating the left side of an assignment, but even better to avoid duplicating long calls on the right side of an assignment, such as a database calls in the following example. Instead of ugly if-then trees (which I run into often):

int x = 0;
YourDatabaseResultSet data = new YourDatabaseResultSet();
if (cond1)
    if (int.TryParse(x_input, x)){
        data = YourDatabaseAccessMethod("my_proc_name", 2, x);
    }
    else{
        x = -1;
        // do something to report "Can't Parse"    
    }
}
else {
    x = y;
    data = YourDatabaseAccessMethod("my_proc_name", 
       new SqlParameter("@param1", 2),
       new SqlParameter("@param2", x));
}

You can do:

int x = cond1 ? (Parser.ParseInt(x_input) ?? -1) : y;
if (x >= 0)  data = YourDatabaseAccessMethod("my_proc_name", 
    new SqlParameter("@param1", 2),
    new SqlParameter("@param2", x));

Much cleaner and easier to understand

Patrick Szalapski
+1. And you could make it an extension method if you don't mind to have one more method to appear in IntelliSense for strings
Roman Boiko
+22  A: 

The Environment.UserInteractive property.

The UserInteractive property reports false for a Windows process or a service like IIS that runs without a user interface. If this property is false, do not display modal dialogs or message boxes because there is no graphical user interface for the user to interact with.

GvS
+1 nice for reducing "progress update" slowdowns
chakrit
Definitely sounds useful for building services or command line utilities.
SkippyFire
@SkippyFire: Not for command-line utilities. A console application is still user-interactive, and can still create message boxes or any other GUI elements, btw. The **only** difference between a so-called "Windows Application" and a "Console Application" is that a console app creates a console window or attaches to one if run a command-line.
P Daddy
+2  A: 

ContextBoundObject

Not so much a C# thing as a .NET thing. It's another way of achieving DI although it can be hardwork. And you have to inherit from it which can be off putting.

http://msdn.microsoft.com/en-us/library/system.contextboundobject.aspx

I've used it to add logging when I decorate a class/method with a custom logging attribute.

objektivs
+23  A: 

Full access to the call stack:

public static void Main()
{
  StackTrace stackTrace = new StackTrace();           // get call stack
  StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)

  // write call stack method names
  foreach (StackFrame stackFrame in stackFrames)
  {
    Console.WriteLine(stackFrame.GetMethod().Name);   // write method name
  }
}

So, if you'll take the first one - you know what function you are in. If you're creating a helper tracing function - take one before the last one - you'll know your caller.

IgorM
One thing that might trip you up is if you're in Debug or Release mode. The stack trace can differ due to optimizations. This screwed me up in an ill-fated attempt to regulate the callers of certain methods: http://moffdub.wordpress.com/2008/07/01/method-regulator-pattern/
moffdub
you can use <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> to help with this problem.
Maslow
@Maslow: Thanks, that helped me solve a problem and make our security system much easier to use.
JohannesH
@moffdub @JohannesH Checking up the stack to see who the caller is for security purposes seems farily dubious. Certainly in C it's trivial to make it look like some benign code is calling you -- it wouldn't surprise me if the same is true of .NET
asveikau
@asveikau: You obviously aren't familiar with the CLR's code access security model or you wouldn't say that. The security on NET Framework CLR's runtime stack is very carefully and tightly controlled, since it forms the basis for NET Framework's security model. Specifically, managed code has absolutely zero ability to manipulate it at all.
Ray Burns
@Ray Burns - Fair enough for you to say I'm not all that familiar with managed code.. However I do recall reading about some exploits of Java's (not .NET but close) stack-based security model. And certainly a naively implemented policy based on a walk of the stack (not as part of the language runtime, but something say in a third-party library as a misguided security feature) has great potential for misuse.
asveikau
+3  A: 

I find this technique interesting while working with linqxml:

public bool GetFooSetting(XElement ndef){
   return (bool?)ndef.Element("MyBoolSettingValue") ?? true;
}

as opposed to:

public bool GetFooSetting(XElement ndef){
   return ndef.Element("MyBoolSettingValue") != null ? bool.Parse(ndef.Element("MyBoolSettingValue") ) : true;
}
sbeskur
+2  A: 

Not sure why anyone would ever want to use Nullable<.bool> though

  1. tic-tac-toe
  2. data access
Greg Dean
Tristates are reasonably common. For instance, in a security policy you could well use false=forbidden; true=allowed; null=continue to next rule
Jon Skeet
... and it should be an enum.
dbkk
Tristates are good for anything that's not supposed to be set until explicitly set. (Anything with an undefined state.) Great for iterating through lists of bool too. All(true) maps to true, All(false) maps to false, neither of the above maps to null.
lc
Given two variables: somethingRequested and requestGranted. somethingRequested is a boolean, requestGranted is also obviously a boolean, but its value is meaningless if somethingRequested isn't true. requestGranted == false means something different if no one made a request to start with.
CMPalmer
Am i wrong in thinking that if a datatype can contain true false and null it by definition isn't a boolean.
Adam Naylor
+106  A: 

If you want to exit your program without calling any finally blocks or finalizers use FailFast:

Environment.FailFast()
Jan Bannister
Would probably be great for an "Emergency Shutdown" button!
SkippyFire
Excellent name for method. :)
Arnis L.
Yes :D Excellent name
Barbaros Alp
Really? You can do this?
George Mauer
I can see the ad for this: "It not only fails, it does it fast!"
Danut Enachioiu
Note that this method also creates a memory dump and writes a message to the Windows error log.
RickNZ
It should be noted no finalizers or app domain unload events will be called when this method is used...
AWC
@Danut Enachioiu: kind of like Windows95/98/Me??? Lol
dboarman
@dboarman: you have to admit blue-screens were *fast*.
Danut Enachioiu
A kill switch when FUBAR
Chenster
+3  A: 
double dSqrd = Math.Pow(d,2.0);

is more accurate than

double dSqrd = d * d; // Here we can lose precision
Jan Bannister
Care to give an example? (Note that there's *always* the possibility of losing precision...)
Jon Skeet
+8  A: 

You can switch on string!

switch(name)
{
  case "Dave":
    return true;
  case "Bob":
    return false;
  default:
    throw new ApplicationException();
}

Very handy! and a lot cleaner than a bunch of if-else statements

Jan Bannister
Wow! That really is surprising -- I always used a hashtable in such a case... Note that underlying CLR code here must be *completely* different than in a usual switch.
dbkk
Yes, it turns it into a bunch of gotos in CLR.
Arda Xi
+48  A: 

Just learned, anonymous types can infer property names from the variable name:

string hello = "world";
var o = new { hello };
Console.WriteLine(o.hello);
Cristian Libardo
that's strange!
chakrit
It's ridiculously useful for LINQ queries
BlueRaja - Danny Pflughoeft
That's really useful. Reminds me of JSON
Chris T
+44  A: 

More of a runtime feature, but I recently learned that there are two garbage collectors. The workstation gc and the server gc. Workstation is the default on client versions of windows, but server is much faster on multicore machines.


<configuration>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
</configuration>

Be careful. The server gc requires more memory.

Mike Two
Nice tip. You should move this question to the Hidden .NET Base Class Library question http://stackoverflow.com/questions/122784/hidden-net-base-class-library-classes
John Sheehan
Awesome. I've got a multicore webserver; its extra core and memory have been going to waste!
tsilb
This optimization works well for me on IronScheme too :)
leppie
+1... never knew that before!
chakrit
On server SKUs of Windows (Server 2003, etc) the default is to use the server GC. The workstation GC is the default on client SKUs such as Vista.
DSO
+4  A: 

This may be pretty basic to database application developers, but it took me a while to realize that null is not the same as DBNull.value.

You have to use DBNull.value when you want to see if a value from a database record is null.

JettGeek
+7  A: 

Object.ReferenceEquals Method

Determines whether the specified Object instances are the same instance.

Parameters:

  • objA: System.Object - The first Object to compare.
  • objB: System.Object - The second Object to compare.

Example:

 object o = null;
 object p = null;
 object q = new Object();

 Console.WriteLine(Object.ReferenceEquals(o, p));
 p = q;
 Console.WriteLine(Object.ReferenceEquals(p, q));
 Console.WriteLine(Object.ReferenceEquals(o, p));

Difference to "==" and ".Equals":

Basically, Equals() tests of object A has the same content as object B.

The method System.Object.ReferenceEquals() always compares references. Although a class can provide its own behavior for the equality operator (below), that re-defined operator isn't invoked if the operator is called via a reference to System.Object.

For strings there isn't really a difference, because both == and Equals have been overriden to compare the content of the string.

See also this answer to another question ("How do I check for nulls in an ‘==’ operator overload without infinite recursion?").

splattne
+64  A: 

Mixins. Basically, if you want to add a feature to several files, but cannot use one base class for all, get each class to implement an interface (with no members). Then, write an extension method for the interface, i.e.

public static DeepCopy(this IPrototype p) { ... }

Of course, some clarity is sacrificed. But it works!

Dmitri Nesteruk
Wow, this almost bringings back multiple inheritance!
Mario
Ooo. Subtle, but I think I could use this to clean up some nastiness I've been fighting with just recently.
Greg D
This is so neat...
Arnis L.
Yeah I think this is the real power of extension methods. They basically allow for implementations of interface methods.
SkippyFire
This is also handy if you're using NHibernate (or Castle ActiveRecord) and you have to use interfaces for your collections. This way you can give behavior to the collection interfaces.
Kyralessa
That's basically how all of the LINQ methods are implemented, for the record.
WCWedin
+3  A: 

ThreadStaticAttribute is a favorite of mine. Also, NonSerializableAttribute is useful. (Can you tell I do a lot of server stuff using remoting?)

dviljoen
+6  A: 

The generic event handler:

public event EventHandler<MyEventArgs> MyEvent;

This way you don't have to declare your own delegates all the time,

+4  A: 

Just learned the joys of [UnmanagedFunctionPointerAttribute(CallingConvention.CDecl)] from trying to interface with an unmanaged C++ function library that defined callbacks without __stdcall.

Dan Fleet
+4  A: 

I didn't discover - for almost a year - that Strongly Typed DataRows contain an Is[ColumnName]Null() method.

For example:

Units.UnitsDataTable dataTable = new Units.UnitsDataTable();

foreach (Units.UnitsRow row in dataTable.Rows)
{
    if (row.IsPrimaryKeyNull())
        //....

    if (row.IsForeignKeyNull())
        //....
}
Gavin Miller
And you just saved me that year!
lc
To be nitpicky, it's not a C# feature, it's a .NET feature ;)
LBugnion
+5  A: 

Math.Max and Min to check boundaries: I 've seen this in a lot of code:

if (x < lowerBoundary) 
{
   x = lowerBoundary;
}

I find this smaller, cleaner and more readable:

x = Math.Max(x, lowerBoundary);

Or you can also use a ternary operator:

x = ( x < lowerBoundary) ? lowerBoundary : x;
Cohen
I think Math.min is much more readable than the ternary operator.
tuinstoel
I personally agree, but apparently not everyone agrees:http://www.refactormycode.com/codes/692-which-is-more-readable-math-or-if
Cohen
Should be 'x = Math.Max(x, lowerBoundary);'
it depends
@it depends: so true, how could I have missed it.. doh! corrected!
Cohen
+11  A: 

Something I missed for a long time: you can compare strings with

"string".equals("String", StringComparison.InvariantCultureIgnoreCase)

instead of doing:

"string".ToLower() == "String".ToLower();
Cohen
I know this should be the correct approach, but I'm annoyed by verbosity of the enum itself.
dbkk
Actualy, StringComparison.OrdinalIgnoreCase should be used in many case.
François
also microsoft has optimized for upper case comparisons so ToUppper would be better on the old way.
Maslow
+6  A: 

Mixins are a nice feature. Basically, mixins let you have concrete code for an interface instead of a class. Then, just implement the interface in a bunch of classes, and you automatically get mixin functionality. For example, to mix in deep copying into several classes, define an interface

internal interface IPrototype<T> { }

Add functionality for this interface

internal static class Prototype
{
  public static T DeepCopy<T>(this IPrototype<T> target)
  {
    T copy;
    using (var stream = new MemoryStream())
    {
      var formatter = new BinaryFormatter();
      formatter.Serialize(stream, (T)target);
      stream.Seek(0, SeekOrigin.Begin);
      copy = (T) formatter.Deserialize(stream);
      stream.Close();
    }
    return copy;
  }
}

Then implement interface in any type to get a mixin.

Dmitri Nesteruk
I get the concept...but..."mixins" makes me think of the ice cream shop. Isn't there some other term for these?
Kyralessa
Is this similiar to what castle dynamic proxy does?
Bless Yahu
They really *are* called mixins. It's a term that comes from C++ where mixing were abstract superclasses or some such. In C#, they are just done using extension methods over interfaces.
Dmitri Nesteruk
Another name for mix-ins is often "trait".
Tommy
+3  A: 

ViewState getters can be one-liners.

Using a default value:

public string Caption
{
    get { return (string) (ViewState["Caption"] ?? "Foo"); }
    set { ViewState["Caption"] = value; }
}

public int Index
{
    get { return (int) (ViewState["Index"] ?? 0); }
    set { ViewState["Index"] = value; }
}

Using null as the default:

public string Caption
{
    get { return (string) ViewState["Caption"]; }
    set { ViewState["Caption"] = value; }
}

public int? Index
{
    get { return (int?) ViewState["Index"]; }
    set { ViewState["Index"] = value; }
}

This works for anything backed by a dictionary.

Bryan Watts
+5  A: 

@Andreas H.R. Nilsson regarding foreach: It does not use 'duck typing', as duck typing IMO refers to a runtime check. It uses structural type checking (as opposed to nominal) at compile time to check for the required method in the type. (Sorry for the new post, I don't have enough points to post comments directly to posts yet.)

Eyvind
+1  A: 

Here is a TIP of how you can use #Region directive to document your code.

+6  A: 

(I just used this one) Set a field null and return it without an intermediate variable:

try
{
    return _field;
}
finally
{
    _field = null;
}
Bryan Watts
Nice hack :), it's clearly not "clear intent" in this code.
Pop Catalin
It's an idiom, like the ternary operator.Try/finally have a temporal relationship, so I think that reads quite nice, once you've seen it before :-)
Bryan Watts
Wow, I love this!
Dmitri Nesteruk
Nice! but can't think of where I'd need one...
chakrit
Hopefully, I will never review your code.var previousValue = _field;_field = null;return previousValue;3 lines, your solution => 8 lines and brainfucking. Man... it remembers me return within finally blocks ^^
François
Your solution is chock full of mechanism, my solution declares intent: return the field, then set the field null. Not sure what's "brainfucked" about that besides your comment :-)
Bryan Watts
Clever, but a difficult read. Most developer's minds go right to try/catch when they see a "try" in the code.Also, I can't think of a lot of situations where this would be useful. There's probably a property to wrap that member, and when in the world would you have a method that was like DoSomestuffToAMemberReturnItToMeThenNullIt()? hehe. Just seems counterintuitive. I'm with Guillaume on this one.
blesh
The usage I mentioned originally was for a builder pattern. At the end of .Build(), I return the member variable holding the instance and null it out to indicate the build is complete. Perfect application of this technique, and no property involved. Also, "try/finally" blocks are pervasive; I don't agree with your assertion that most developers immediately think of "try/catch" when they see "try".
Bryan Watts
+6  A: 

Explicit interface member implementation, wherein an interface member is implemented, but hidden unless the instance is cast to the interface type.

Rytmis
+26  A: 

On-demand field initialization in one line:

public StringBuilder Builder
{
    get { return _builder ?? (_builder = new StringBuilder()); }
}

I'm not sure how I feel about C# supporting assignment expressions, but hey, it's there :-)

Bryan Watts
I'm disappointed, so many years coding in .NET without using this syntax... Great !
Nicolas Dorier
Unfortunately this feature is duped. Saw the same above.
Arnis L.
It isn't anymore in .NET 3.5.
Joop
I'm fairly certain you are mistaken. I didn't see anything on a Google search and I would be very surprised if C# made a breaking syntax change of any kind.
Bryan Watts
duplicate: http://stackoverflow.com/questions/9033/hidden-features-of-c/58945#58945
sdcvvc
I don't think I like this, especially for privates. It's a good technique for exposing publics though. I'd prefix it with 'Summon' though, for instance, SummonBuilder (http://stevedunns.blogspot.com/2010/04/summon-method.html)
Steve Dunn
+20  A: 

Several people have mentioned using blocks, but I think they are much more useful than people have realised. Think of them as the poor man's AOP tool. I have a host of simple objects that capture state in the constructor and then restore it in the Dispose() method. That allows me to wrap a piece of functionality in a using block and be sure that the state is restored at the end. For example:

using(new CursorState(this, BusyCursor));
{
    // Do stuff
}

CursorState captures the current cursor being used by form, then sets the form to use the cursor supplied. At the end it restores the original cursor. I do loads of things like this, for example capturing the selections and current row on a grid before refreshing and so on.

open-collar
I used this trick once, and it ended up being useless because Win32 does something to restore the cursor for you, at least in WinForms
Olmo
I've just declared class called DisposableAction that takes action in constructor and executes it on the disposal. Really convenient.http://build.lokad.com/doc/shared/html/E5642EA0.htm
Rinat Abdullin
It is a bit cleaner than the try-finally approach that I find myself using.
Greg D
@open-collar: that is convenient...good to know... +1
dboarman
+13  A: 

You can use any Unicode character in C# names, for example:

public class MyClass
{
    public string Hårføner()
    {
        return "Yes, it works!";
    }
}

You can even use Unicode escapes. This one is equivalent to the above:

public class MyClass
{
    public string H\u00e5rføner()
    {
        return "Yes, it (still) works!";
    }
}
Tor Haugen
Unicode escapes?!? lol that is one cool feature.
chakrit
Awesome! That's just fun!
Sorskoot
To anyone outside the english-speaking world, it's actually a giant leap towards language-agnostic computing. To us (to me, at least), it's actually somewhat quaint and old-fashioned to have to stick to the 26 letters of the english alphabet. A bit like 8.3 file names, you know.
Tor Haugen
Hmm, yes, let's mix English BCL identifiers and keywords with non-English identifiers. Now people from other countries can't use your code anymore without Intellisense :P ;) No. It's only consequent to be able to use any character, but there's no real benefit to it.
OregonGhost
I loved this one!! awesome
Prashant
Although some might find this useful I think it should be avoided in our industry since we will often have several nationalities involved in a project. Also as OregonGhost mentiones it will go against existing naming in BCL and 3rd party libraries.I'm currently working on a project which didn't use english naming and we are now suffering from confusing names as well as the inability to hand over our code to our indian testers without extensive documentation and transcription of member names. Remember that code should be readable by humans... This includes humans of other nationalities.
JohannesH
@JohanneshH: That's what you get for exploiting the impoverished nations! :P
ANeves
+1 for the ludicrously obvious problems with character encoding (XAML goes nuts with such things)
ANeves
AMAZING feature non-english speakers dreamt of for many years. Though dangerous it makes learning programming language a great fun.
SlavaGu
+12  A: 

What about using this:

#if DEBUG
            Console.Write("Debugging");
#else
            Console.Write("Final");
#endif

When you have your solution compiled with DEBUG defined it will output "Debugging".

If your compile is set to Release it will write "Final".

MysticSlayer
+4  A: 

Framework Feature

I don't know but I was quite suprised about VisualStyleRenderer and the whole System.Windows.Forms.VisualStyles-Namespace. Pretty cool!

Gregor
+9  A: 

IEnumerable's SelectMany, which flattens a list of lists into a single list. Let's say I have a list of Orders, and each Order has a list of LineItems on that order.

I want to know the total number of LineItems sold...

int totalItems = Orders.Select(o => o.LineItems).SelectMany(i => i).Sum();
Richard Ev
int totalItems = Orders.SelectMany(o => o.LineItems).Sum();
Pop Catalin
+7  A: 

This isn't a C# specific type, but I just found the ISurrogateSelector and ISerializationSurrogate interfaces --

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.isurrogateselector.aspx

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.isurrogateselector.aspx

Using these in conjunction with the BinaryFormatter allows for non-serializable objects to be serialized via the implementation of a surrogate class. The surrogate pattern is well-understood in computer science, particularly when dealing with the problem of serialization. I think that this implementation is just tucked away as a parameter of the constructor to BinaryFormatter, and that's too bad.

Still - VERY hidden. :)

Rob
some example on how to use it?
chakrit
+9  A: 

The delegate syntax have evolved over successive versions of C#, but I still find them difficult to remember. Fortunately the Action<> and Func<> delegates are easy to remember.

For example:

  • Action<int> is a delegate method that takes a single int argument and returns void.
  • Func<int> is a delegate method that takes no arguments and returns an int.
  • Func<int, bool> is a delegate method that takes a single int argument and returns a bool.

These features were introduced in version 3.5 of the .NET framework.

Phillip Ngan
Action<T> is available in .NET 2.0. It's a shame that Func isn't, because it would make using lambdas easier.
OregonGhost
dboarman
A few of them: Action, Comparison, Converter, EventHandler, Func, Predicate (see http://stackoverflow.com/questions/319789/whats-so-great-about-func-delegate/319929#319929 )
ANeves
+16  A: 

One great class I like is System.Xml.XmlConvert which can be used to read values from xml tag. Especially, if I am reading a boolean value from xml attribute or element, I use

bool myFlag  = System.Xml.XmlConvert.ToBoolean(myAttribute.Value);

Note: since boolean type in xml accepts 1 and 0 in addition to "true" and "false" as valid values, using string comparison in this case is error-prone.

+37  A: 

Other underused operators are checked and unchecked:

short x = 32767;   // 32767 is the max value for short
short y = 32767;
int z1 =  checked((short)(x + y));   //will throw an OverflowException
int z2 =  unchecked((short)(x + y)); // will return -2
int z3 =  (short)(x + y);            // will return -2
Binoj Antony
Instead of 32767 and a comment, how about `short.MaxValue`?
clintp
Just reminding everyone what the exact MaxValue is!
Binoj Antony
+9  A: 

To call the base class constructor just put base() inline with the constructor.
To call the base class method you can just put base.MethodName() inside the derived class method

class ClassA 
{
  public ClassA(int a)
  {
    //Do something
  }

  public void Method1()
  {
     //Do Something
  }
}

class ClassB : ClassA
{
  public ClassB(int a) : base(a) // calling the base class constructor
  {
    //Do something
  }

  public void Method2()
  {
    base.Method1();               // calling the base class method
  }
}

Of course you can call the methods of the base class by just saying base.MethodName()

Binoj Antony
+7  A: 

dynamic keyword in C# 4.0

You can use dynamic keyword, if you want your method calls to be resolved only at the runtime.

dynamic invoker=new DynamicInvoker();
dynamic result1=invoker.MyMethod1();
dynamic result2=invoker.MyMethod2();

Here I'm implementing a dynamic invoker.

public class DynamicInvoker : IDynamicObject
    {
        public MetaObject GetMetaObject
              (System.Linq.Expressions.Expression parameter)
        {
            return new DynamicReaderDispatch (parameter);
        }
    }

    public class DynamicDispatcher : MetaObject
    {
        public DynamicDispatcher (Expression parameter) 
                   : base(parameter, Restrictions.Empty){ }

        public override MetaObject Call(CallAction action, MetaObject[] args)
        {
            //You'll get MyMethod1 and MyMethod2 here (and what ever you call)
            Console.WriteLine("Logic to invoke Method '{0}'", action.Name);
            return this; //Return a meta object
        }
    }
amazedsaint
+14  A: 

Ability to use LINQ Expressions to perform strongly-typed reflection:

static void Main(string[] args)
{
  var domain = "matrix";
  Check(() => domain);
  Console.ReadLine();
}

static void Check<T>(Expression<Func<T>> expr)
{
  var body = ((MemberExpression)expr.Body);
  Console.WriteLine("Name is: {0}", body.Member.Name);
  Console.WriteLine("Value is: {0}", ((FieldInfo)body.Member)
   .GetValue(((ConstantExpression)body.Expression).Value));
}

// output:
// Name is: 'domain'
// Value is: 'matrix'

More details are available at How to Find Out Variable or Parameter Name in C#?

Rinat Abdullin
+6  A: 

This isn't a C# specific feature but it is an addon that I find very useful. It is called the Resource Refactoring Tool. It allows you to right click on a literal string and extract it into a resource file. It will search the code and find any other literal strings that match and replace it with the same resource from the Resx file.

http://www.codeplex.com/ResourceRefactoring

Steven Behnke
+9  A: 

TrueForAll Method of List<T> :

List<int> s = new List<int> { 6, 1, 2 };

bool a = s.TrueForAll(p => p > 0);
Moran
+29  A: 

Closures

Since anonymous delegates were added to 2.0, we have been able to develop closures. They are rarely used by programmers but provide great benefits such as immediate code reuse. Consider this piece of code:

bool changed = false;

if (model.Prop1 != prop1)
{
    changed = true;
    model.Prop1 = prop1;
}
if (model.Prop2 != prop2)
{
    changed = true;
    model.Prop2 = prop2;
}
// ... etc.

Note that the if-statements above perform similar pieces of code with the exception of one line of code, i.e. setting different properties. This can be shortened with the following, where the varying line of code is entered as a parameter to an Action object, appropriately named setAndTagChanged:

bool changed = false;
Action<Action> setAndTagChanged = (action) => 
{ 
    changed = true; 
    action(); 
};

if (model.Prop1 != prop1) setAndTagChanged(model.Prop1 = prop1);
if (model.Prop2 != prop2) setAndTagChanged(model.Prop2 = prop2);

In the second case, the closure allows you to scope the change variable in your lambda, which is a concise way to approach this problem.

An alternate way is to use another unused feature, the "or equal" binary assignment operator. The following code shows how:

private bool conditionalSet(bool condition, Action action)
{
    if (condition) action();
    return condition;
}

// ...

bool changed = false;
changed |= conditionalSet(model.Prop1 == prop1, model.Prop1 = prop1);
changed |= conditionalSet(model.Prop2 == prop2, model.Prop2 = prop2);
Michael Meadows
+1 I use it like this a lot
chakrit
I might argue with the "rarely used" comment. I use them all the time. :)
Greg D
I'm not having any luck getting the 2nd one to compile? is it .net 3.5 only? public int eye { get; set; } private void testClosure() { var i = 0; bool changed = false; Action<Action> setAndTagChanged = (action) => { changed = true; action(); }; setAndTagChanged(eye=1); }
Maslow
Lambdas are only available in C# 3.0 or greater. That means you must have .Net 3.5.
Michael Meadows
Thanks, luckily we've moved up to .net 3.5 thanks to my pushing for it
Maslow
+1 Link to closures question:http://stackoverflow.com/questions/428617/closures-in-netArticle with explanation of compiler generated code http://blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspx
chapluck
+6  A: 

I call this AutoDebug because you can drop right into debug where and when you need based on a bool value which could also be stored as a project user setting as well.

Example:

//Place at top of your code
public UseAutoDebug = true;


//Place anywhere in your code including catch areas in try/catch blocks
Debug.Assert(!this.UseAutoDebug);

Simply place the above in try/catch blocks or other areas of your code and set UseAutoDebug to true or false and drop into debug anytime you wish for testing.

You can leave this code in place and toggle this feature on and off when testing, You can also save it as a Project Setting, and manually change it after deployment to get additional bug information from users when/if needed as well.

You can see a functional and working example of using this technique in this Visual Studio C# Project Template here, where it is used heavily:

http://code.msdn.microsoft.com/SEHE

+16  A: 

C# 3.0's LINQ query comprehensions are full-blown monadic comprehensions a la Haskell (in fact they were designed by one of Haskell's designers). They will work for any generic type that follows the "LINQ pattern" and allows you to write in a pure monadic functional style, which means that all of your variables are immutable (as if the only variables you used were IDisposables and IEnumerables in using and foreach statements). This is helpful for keeping variable declarations close to where they're used and making sure that all side-effects are explicitly declared, if there are any at all.

 interface IFoo<T>
  { T Bar {get;}
  }

 class MyFoo<T> : IFoo<T> 
  { public MyFoo(T t) {Bar = t;}
    public T Bar {get; private set;} 
  }

 static class Foo 
  { public static IFoo<T> ToFoo<T>(this T t) {return new MyFoo<T>(t);}

    public static void Do<T>(this T t, Action<T> a) { a(t);}

    public static IFoo<U> Select<T,U>(this IFoo<T> foo, Func<T,U> f) 
     { return f(foo.Bar).ToFoo();
     }
  }

 /* ... */

 using (var file = File.OpenRead("objc.h"))
 { var x = from f in file.ToFoo()
           let s = new Scanner(f)
           let p = new Parser {scanner = s}
           select p.Parse();

   x.Do(p => 
    { /* drop into imperative code to handle file 
         in Foo monad if necessary */      
    });

 }
Mark Cidade
This makes me feel dumb, I need to learn some Haskell, I guess!
Greg D
+29  A: 

I'd say using certain system classes for extension methods is very handy, for example System.Enum, you can do something like below...

[Flags]
public enum ErrorTypes : int {
    None = 0,
    MissingPassword = 1,
    MissingUsername = 2,
    PasswordIncorrect = 4
}

public static class EnumExtensions {

    public T Append<T>(this System.Enum type, T value) {
        return (T)(object)(((int)(object)type | (int)(object)value));
    }

    public static T Remove<T>(this System.Enum type, T value) {
        return (T)(object)(((int)(object)type & ~(int)(object)value));
    }

    public static bool Has<T>(this System.Enum type, T value) {
        return (((int)(object)type & (int)(object)value) == (int)(object)value);
    }

}

...

//used like the following...

ErrorTypes error = ErrorTypes.None;
error = error.Append(ErrorTypes.MissingUsername);
error = error.Append(ErrorTypes.MissingPassword);
error = error.Remove(ErrorTypes.MissingUsername);

//then you can check using other methods
if (error.Has(ErrorTypes.MissingUsername)) {
    ...
}

This is just an example of course - the methods could use a little more work...

Hugoware
Great ! Now your code is mine ;p
Nicolas Dorier
Very interesting usage. I like this syntax a lot better than the bitwise operator syntax.
SkippyFire
+1 Brilliant suggestion. Significantly improves readability.
Bernhof
Nice; but I'd use Include instead of Append since Append implies an order which the values may not have.
devstuff
@devstuff - that is a good point, I didn't really ever think about it from that perspective.
Hugoware
@HBoss: I'm gonna use that. It will make it so much easier to implement Flags the way we should be.
dboarman
+7  A: 

You can have generic methods in a non-generic class.

JohnOpincar
+6  A: 

Cool trick to emulate functional "wildcard" arguments (like '_' in Haskell) when using lambdas:

(_, b, __) => b.DoStuff();  // only interested in b here
Andrew Peters
Not really a trick, just a naming choice. I think it looks daft since you're forced to use increasing numbers of underscores.
frou
+3  A: 

Only for reference - enum binary operations using the extension method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace BinaryOpGenericTest
{
    [Flags]
    enum MyFlags
    {
        A = 1,
        B = 2,
        C = 4

    }

    static class EnumExtensions
    {
        private static Dictionary<Type, Delegate> m_operations = new Dictionary<Type, Delegate>();

        public static bool IsFlagSet<T>(this T firstOperand, T secondOperand) 
                                                  where T : struct
        {

            Type enumType = typeof(T);


            if (!enumType.IsEnum)
            {
                throw new InvalidOperationException("Enum type parameter required");
            }


            Delegate funcImplementorBase = null;
            m_operations.TryGetValue(enumType, out funcImplementorBase);

            Func<T, T, bool> funcImplementor = funcImplementorBase as Func<T, T, bool>;

            if (funcImplementor == null)
            {
                funcImplementor = buildFuncImplementor(secondOperand);
            }



            return funcImplementor(firstOperand, secondOperand);
        }


        private static Func<T, T, bool> buildFuncImplementor<T>(T val)
                                                            where T : struct
        {
            var first = Expression.Parameter(val.GetType(), "first");
            var second = Expression.Parameter(val.GetType(), "second");

            Expression convertSecondExpresion = Expression.Convert(second, typeof(int));
            var andOperator = Expression.Lambda<Func<T, T, bool>>(Expression.Equal(
                                                                                                       Expression.And(
                                                                                                            Expression.Convert(first, typeof(int)),
                                                                                                             convertSecondExpresion),
                                                                                                       convertSecondExpresion),
                                                                                             new[] { first, second });
            Func<T, T, bool> andOperatorFunc = andOperator.Compile();
            m_operations[typeof(T)] = andOperatorFunc;
            return andOperatorFunc;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyFlags flag = MyFlags.A | MyFlags.B;

            Console.WriteLine(flag.IsFlagSet(MyFlags.A));            
            Console.WriteLine(EnumExtensions.IsFlagSet(flag, MyFlags.C));
            Console.ReadLine();
        }
    }
}
+7  A: 

Method groups aren't well known.

Given:

Func<Func<int,int>,int,int> myFunc1 = (i, j) => i(j);
Func<int, int> myFunc2 = i => i + 2;

You can do this:

var x = myFunc1(myFunc2, 1);

instead of this:

var x = myFunc1(z => myFunc2(z), 1);
Jonathan Parker
Which is the same as saying, given `List<int> items = { 1, 3, 5, 7, 11} ;`, that you can do `var total = items.Sum(myFunc2);` instead of `var total = items.Sum(i => myFunc2(i));`.
ANeves
+9  A: 

You can use generics to check (compile time) if a method argument implements two interfaces:

interface IPropA 
{
    string PropA { get; set; } 
}

interface IPropB 
{
    string PropB { get; set; }
}

class TestClass 
{
    void DoSomething<T>(T t) where T : IPropA, IPropB 
    {
        MessageBox.Show(t.PropA);
        MessageBox.Show(t.PropB);
    }
}

Same with an argument that is inherited from a base class and an interface.

tuinstoel
In this case, would you need to do var a = (IPropA)t; var propa = a.PropA; Just in case the interface is explicitly implemented?
Mark
@Mark, I don't understand your question so I can't answer it.
tuinstoel
@Mark: No, you don't have to cast it. The fact that `t` implements the given interfaces is known explicitly from the `where` (in fact, that is all that is known, other than that `t` is an object), so the cast is unnecessary. When t.PropA is accessed, it works exactly as if t were a non-generic parameter of type IPropA, and when t.PropB is accessed, it works exactly as if t were a non-generic parameter of type IPropB.
P Daddy
@tuinstoel: +1! This is an excellent demonstration of functionality that's not possible without generics.
P Daddy
+5  A: 

Is constructor chain already cited?

namespace constructorChain {
    using System;

    public class Class1 {
        public string x;
        public string y;

        public Class1() {
            x = "class1";
            y = "";
        }

        public Class1(string y)
            : this() {
            this.y = y;
        }
    }

    public class Class2 : Class1 {
        public Class2(int y)
            : base(y.ToString()) {

        }
    }
}

...

        constructorChain.Class1 c1 = new constructorChain.Class1();
        constructorChain.Class1 c12 = new constructorChain.Class1("Hello, Constructor!");
        constructorChain.Class2 c2 = new constructorChain.Class2(10);
        Console.WriteLine("{0}:{1}", c1.x, c1.y);
        Console.WriteLine("{0}:{1}", c12.x, c12.y);
        Console.WriteLine("{0}:{1}", c2.x, c2.y);

        Console.ReadLine();
kentaromiura
when can this be useful?
Peter Gfader
Everytime you have constructors that contains the same initialization code, constructor chaining avoids code duplication.
kentaromiura
A: 

Not hidden, but pretty neat. I find this a more succinct substitute for a simple if-then-else that just assigns a value based on a condition.

string result = 
              i < 2 ?               //question
              "less than 2" :       //answer
              i < 5 ?               //question
             "less than 5":         //answer   
              i < 10 ?              //question
              "less than 10":       //answer
              "something else";     //default answer
Raghu Dodda
However, the comparisons are either in the wrong order (i.e. compare 2, then 5, then 10) or they are the comparison is the wrong direction (i.e. test for greater than instead of less than). When i = 1, it will set result to "less than 10".
Mark
That happens when you use such an illegible construct.
Juanma
Mark - Thanks for point that out. I corrected it.
Raghu Dodda
if statements are far more readable than that. This version simply gives you street cred :)
Sir Psycho
Just use an if statement man!
Groky
+7  A: 

Here's one I discovered recently which has been useful:

Microsoft.VisualBasic.Logging.FileLogTraceListener

MSDN Link

This is a TraceListener implementation which has a lot of features, such as automatic log file roll over, which I previously would use a custom logging framework for. The nice thing is that it is a core part of .NET and is integrated with the Trace framework, so its easy to pick up and use immediately.

This is "hidden" because its in the Microsoft.VisualBasic assembly... but you can use it from C# as well.

DSO
A lot of people use log4net or the enterprises library's log features. Do you think that Microsoft.VisualBasic.Logging.FileLogTraceListener is a better choice?
tuinstoel
Probably not as full featured as those other ones. But to repeat myself:"The nice thing is that it is a core part of .NET and is integrated with the Trace framework, so its easy to pick up and use immediately."
DSO
+8  A: 

Working with enums.

Convert a string to an Enum:

enum MyEnum
{
    FirstValue,
    SecondValue,
    ThirdValue
}

string enumValueString = "FirstValue";
MyEnum val = (MyEnum)Enum.Parse(typeof(MyEnum), enumValueString, true)
  • I use this to load the value of CacheItemPriority in my ASP.NET applications from a settings table in a database so that I can control caching (along with other settings) dynamically without taking the application down.

When comparing variables of type enum, you don't have to cast to int:

MyEnum val = MyEnum.SecondValue;
if (val < MyEnum.ThirdValue)
{
    // Do something
}
Waleed Eissa
good one, I loved it !!!
Prashant
+9  A: 

I quite enjoy implicit generic parameters on functions. For example, if you have:

public void DoStuff<T>(T value);

Instead of calling it like this:

DoStuff<int>(5);

You can:

DoStuff(5);

And it'll work out the generic type from the parameter's type.

  • This doesn't work if you're calling the method through reflection.
  • I remember having some weird problems on Mono.
Gorkem Pacaci
I enjoy that as well.. its nifty ;)
Arcturus
But it works only with one type parameter. As soon as you add two, it will force you to explicitely specify the Types.
Hermann
+52  A: 

Many people don't realize that they can compare strings using: OrdinalIgnoreCase instead of having to do someString.ToUpper(). This removes the additional string allocation overhead.

if( myString.ToUpper() == theirString.ToUpper() ){ ... }

becomes

if( myString.Equals( theirString, StringComparison.OrdinalIgnoreCase ){ ... }
Ralph
This could be changed quite easily to be null-safe as well: var isEqual = String.Equals(a, b, StringComparison.OrdinalIgnoreCase);
Robert Giesecke
+13  A: 

My favorite attribute: InternalsVisibleTo

At assembly level you can declare that another assembly can see your internals. For testing purposes this is absolutely wonderful.

Stick this in your AssemblyInfo.cs or equivalent and your test assembly get full access to all the internal stuff that requires testing.

[assembly: InternalsVisibleTo("MyLibrary.Test, PublicKey=0024...5c042cb")]

As you can see, the test assembly must have a strong name to gain the trust of the assembly under test.

Available in .Net Framework 2.0+, Compact Framework 2.0+ and XNA Framework 1.0+.

Peter Lillevold
Yeah, great for test assemblies... And only test assemblies. Right? ;)
JohannesH
@JohannesH - Yes, I would agree. There is a good reason for having the internal keyword, and that is to keep things internal :)
Peter Lillevold
+3  A: 

FIXED / Power of Pointers in C# - This topic is too big, but I will just outline simple things.

In C we had facility of loading structure like...

struct cType{
   char type[4];
   int  size;
   char name[50];
   char email[100];
}

cType myType;
fread(file, &mType, sizeof(mType));

We can use fixed keyword in "unsafe" method to read byte array aligned structure.

[Layout(LayoutKind.Sequential, Pack=1)]
public unsafe class CType{
    public fixed byte type[4];
    public int size;
    public fixed byte name[50];
    public fixed byte email[100];
}

Method 1 (Reading from regular stream in to byte buffer and mapping byte array to individual bytes of struct)

CType mType = new CType();
byte[] buffer = new byte[Marshal.SizeOf(CType)];
stream.Read(buffer,0,buffer.Length);
// you can map your buffer back to your struct...
fixed(CType* sp = &mType)
{
    byte* bsp = (byte*) sp;
    fixed(byte* bp = &buffer)
    {
         for(int i=0;i<buffer.Length;i++)
         {
             (*bsp) = (*bp);
             bsp++;bp++;
         }
    }
}

Method 2, you can map Win32 User32.dll's ReadFile to directly read bytes...

CType mType = new CType();
fixed(CType* p = &mType)
{
    User32.ReadFile(fileHandle, (byte*) p, Marshal.SizeOf(mType),0);
}
Akash Kava
+6  A: 

The usage of the default keyword in generic code to return the default value for a type.

public class GenericList<T>
{
    private class Node
    {
        //...

        public Node Next;
        public T Data;
    }

    private Node head;

    //...

    public T GetNext()
    {
        T temp = default(T);

        Node current = head;
        if (current != null)
        {
            temp = current.Data;
            current = current.Next;
        }
        return temp;
    }
}

Another example here

Elroy
+4  A: 

The InternalsVisibleToAttribute specifies that types that are ordinarily visible only within the current assembly are visible to another assembly. Article on msdn

Dries Van Hansewijck
+5  A: 

Instead of doing something cheesy like this:

Console.WriteLine("{0} item(s) found.", count);

I use the following inline trick:

Console.WriteLine("{0} item{1} found.", count, count==1 ? "" : "s");

This will display "item" when there's one item or "items" when there are more (or less) than 1. Not much effort for a little bit of professionalism.

rein
THis is fine for debugging but you'll hit a complete nightmare when you want to get into I18N and L10N
Jeff Yates
sweet solution to this often encountered issue
Peter Gfader
yeah! internationalization will be a horror, but you could do the following: Console.WriteLine("{0} {1} found.", count, count==1 ? "item" : "items");
Peter Gfader
@Peter I'm assuming you meant: Console.WriteLine("{0} {1}", count, count==1 ? "item found." : "items found.");
rein
I use a Pluralise(value, singularname, pluralname) method that spits out the entire "5 items" string. This is much more readable, supports "goose/geese" pluralisation and is much easier to find and deal with when it comes to localisation.
Jason Williams
+9  A: 

One thing not many people know about are some of the C#-introduced preprocessor directives. You can use #error This is an error. to generate a compiler error and #warning This is a warning.

I usually use these when I'm developing with a top-down approach as a "todo" list. I'll #error Implement this function, or #warning Eventually implement this corner case as a reminder.

impulse3d
These are good but I recently discovered that #warning in Silverlight doesn't stop the build when Warnings as Errors is set, even though it should.
Jeff Yates
Can't you just use TODO: comments in Visual Studio? http://dotnetperls.com/todo-comments-visual-studio
Dan Diplo
+2  A: 

In dealing with interop between C++ and C#, many people don't realize that C++/CLI is a great option.

Say you have a C++ DLL and a C# DLL which depends on the C++ DLL. Often, the easiest technique is to compile some (or all) modules of the C++ DLL with the /clr switch. To have the C# call the C++ DLL is to write managed C++ wrapper classes in the C++ DLL. The C++/CLI classes can call the native C++ code much more seamlessly than C#, because the C++ compiler will automatically generate P/invokes for you, has a library specifically for interop, plus language features for interop like pin_ptr. And it allows managed and native code to coexist within the same binary.

On the C# side, you just call into the DLL as you would any other .NET binary.

Drew Hoskins
+6  A: 

I am so so late to this question, but I wanted to add a few that I don't think have been covered. These aren't C#-specific, but I think they're worthy of mention for any C# developer.

AmbientValueAttribute

This is similar to DefaultValueAttribute, but instead of providing the value that a property defaults to, it provides the value that a property uses to decide whether to request its value from somewhere else. For example, for many controls in WinForms, their ForeColor and BackColor properties have an AmbientValue of Color.Empty so that they know to get their colors from their parent control.

IsolatedStorageSettings

This is a Silverlight one. The framework handily includes this sealed class for providing settings persistence at both the per-application and per-site level.

Flag interaction with extension methods

Using extension methods, flag enumeration use can be a lot more readable.

    public static bool Contains(
          this MyEnumType enumValue,
          MyEnumType flagValue)
    {
        return ((enumValue & flagValue) == flagValue);
    }

    public static bool ContainsAny(
          this MyEnumType enumValue,
          MyEnumType flagValue)
    {
        return ((enumValue & flagValue) > 0);
    }

This makes checks for flag values nice and easy to read and write. Of course, it would be nicer if we could use generics and enforce T to be an enum, but that isn't allowed. Perhaps dynamic will make this easier.

Jeff Yates
+7  A: 

The built-in (2.0) MethodInvoker delegate is useful when you want to Invoke/BeginInvoke inline code. This avoids needing to create an actual delegate and separate method.

    void FileMessageEvent(object sender, MessageEventArgs e)
    {

        if (this.InvokeRequired == true)
        {
            this.BeginInvoke((MethodInvoker)delegate { 
                     lblMessage.Text=e.Message; 
                     Application.DoEvents(); 
                 }
            ); 

        }
    }

Resolves the error: "Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type".

Ash
+5  A: 

I find it incredible what type of trouble the compiler goes through to sugar code the use of Outer Variables:

string output = "helo world!";
Action action = () => Console.WriteLine(output);
output = "hello!";
action();

This actually prints hello!. Why? Because the compiler creates a nested class for the delegate, with public fields for all outer variables and inserts setting-code before every single call to the delegate :) Here is above code 'reflectored':

Action action;
<>c__DisplayClass1 CS$<>8__locals2;
CS$<>8__locals2 = new <>c__DisplayClass1();
CS$<>8__locals2.output = "helo world!";
action = new Action(CS$<>8__locals2.<Main>b__0);
CS$<>8__locals2.output = "hello!";
action();

Pretty cool I think.

Josef
May I also point out that Java for instance doesn't even allow the use of non-final outer variables in their anonymous methods...
Josef
+5  A: 

I couldn't figure out what use some of the functions in the Convert class had (such as Convert.ToDouble(int), Convert.ToInt(double)) until I combined them with Array.ConvertAll:

int[] someArrayYouHaveAsInt;
double[] copyOfArrayAsDouble = Array.ConvertAll<int, double>(
                                someArrayYouHaveAsInt,
                                new Converter<int,double>(Convert.ToDouble));

Which avoids the resource allocation issues that arise from defining an inline delegate/closure (and slightly more readable):

int[] someArrayYouHaveAsInt;
double[] copyOfArrayAsDouble = Array.ConvertAll<int, double>(
                                someArrayYouHaveAsInt,
                                new Converter<int,double>(
                                  delegate(int i) { return (double)i; }
                                ));
scraimer
I use Convert.ToDouble, ToInt, etc all the time, not especially for arrays... It's really handy because if an object can be converted one way or another, it will, which is not the case with a cast : for instance you can't cast a string to a Double, you have to use Double.Parse. Or a variable of type object, containing a Decimal, can't be cast to int directly. Convert.ToDouble or ToInt will use the most appropriate way to convert the value, whatever it is.
Thomas Levesque
+6  A: 

Array initialization without specifying the array element type:

var pets = new[] { "Cat", "Dog", "Bird" };
JoshL
also: `string[] pets = {"Cat", "Dog", "Bird"};`
P Daddy
+3  A: 

Well... Don't use it, but a lot of people don't know C# supports the evil goto:)

static void Example()
{
    int i = 0;
top:
    Console.WriteLine(i.ToString());
    if (i == 0)
    {
        i++;
        goto top;
    }
}
Oorang
If you use Reflector, you will see that there are quite a lot of gotos in the .NET Framework code... scary !
Thomas Levesque
Oorang shivers...
Oorang
@Thomas: Most gotos you see in Reflector are just failures of Reflector to correctly detect the while, do/while, for, etc. originally used. Switch statements, in particular, tend to lead to a bunch of "goto Label_01BE", and the like. If you follow through the logic flow, you can usually determine that the source code was probably better structured. And now that the source code for much of the framework is available (http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx), you can see for yourself.
P Daddy
@Oorang: I'd rather see a well-placed goto now and then than more complicated code that tries to avoid it. I've only rarely needed it, and most uses have been of the `goto case...` variety within a switch statement, but it's good to have it when it's what you need. Goto's inclusion in the language is for those occasional times when it really *is* the clearest construct, not to be avoided at all costs.
P Daddy
I've found goto quite useful in some console input situations.
Skurmedel
I do use itsomethimes you have a function with a final action you don't want to do return in the middle of instead you go to the end, the ret value is declared on top as null
Shimmy
I'm often a defender of goto (ie breaking out of nested loops) but I'll be honest... That just sounds like a structure that could be improved.
Oorang
+5  A: 

Having just learned the meaning of invariance, covariance and contravariance, I discovered the in and out generic modifiers that will be included in .NET 4.0. They seem obscure enough that most programmers would not know about them.

There's an article at Visual Studio Magazine which discusses these keywords and how they will be used.

emddudley
+16  A: 

Atrribute Targets

Everyone has seen one. Basically, when you see this:

[assembly: ComVisible(false)]

The "assembly:" portion of that attribute is the target. In this case, the attribute is applied to the assembly, but there are others:

[return: SomeAttr]
int Method3() { return 0; }

In this sample the attribute is applied to the return value.

Dave Van den Eynde
Hey, that's nice!
Markus
+2  A: 

If you want to prevent the garbage collector from running the finalizer of an object, just use GC.SuppressFinalize(object);. In a similar vein, GC.KeepAlive(object); will prevent the garbage collector from collecting that object by referencing it. Not very commonly used, at least in my experience, but nice to know just in case.

Showtime
+7  A: 

Properties to display when viewing components Properties in design view:

private double _Zoom = 1;

[Category("View")]
[Description("The Current Zoom Level")]
public double Zoom
{
get { return _Zoom;}
set { _Zoom = value;}
}

Makes things a lot easier for other users of your component libraries.

ThePower
I haven't made any components so far, so this is a place I wish I could bookmark responses instead of just the question
Maslow
+2  A: 

Relection is so powerfull when used carefully. I used it in an e-mail templating system. The template manager would be passed an object and the html templates would have embedded fields that referred to Properties that could be retrieved off the passed object using reflection. Worked out very nicely.

Jeffrey Hines
+3  A: 

Definitely the Func<> types when used with statement lambdas in .NET 3.5. These allow customizable functions, and can be a great aid in offering user customizable objects without subclassing them or resorting to some limited system like keeping track of a variable that lists what button or key the user wants to monitor. Also, they can be called just like regular methods and can be assigned like variables. The only downside that I can think of is that you're limited to 5 arguments! Although by that point you might want to consider a different solution... Edit: Providing some examples.

...
public Func<InputHelper, float> _horizontalCameraMovement = (InputHelper input) => 
{
    return (input.LeftStickPosition.X * _moveRate) * _zoom;
}
public Func<InputHelper, float> _verticalCameraMovement = (InputHelper input) => 
{
    return (-input.LeftStickPosition.Y * _moveRate) * _zoom;
}
...
public void Update(InputHelper input)
{
    ...
    position += new Vector2(_horizontalCameraMovement(input), _verticalCameraMovement(input));
    ...
}

In this example, you can write a function that does arbitrary calculation and returns a float that will determine the amount that the camera moves by. Not the best code but it gets the point across.

private int foo;
public int FooProperty {
    get
    {
        if (_onFooGotten() == true)
            return _foo;
    }
    set
    {
        if (onFooSet() == true)
            _foo = value;
    }
}
...
public Func<bool> _onFooGotten = () => 
{
    //do whatever...
    return true;
}
public Func<bool> _onFooSet = () =>
{
    //do whatever...
    return true;
}

This isn't the best example (as I haven't really explored this use yet) but it shows an example of using a lambda function for a quick event raiser without the hassle of delegates. Edit: thought of another one. Nullables! The closest thing C# has to optional parameters.

RCIX
Well if Func<> is so great, why don't you provide some nice examples?
tuinstoel
For instance, if you have a 2D camera class and you want to allow the user to control exactly how the various aspects of control are handled (like movement or zooming) you can use Funcs. Or if you want a single-subscriber event and don't want to bother with the effort of delegates. Basically anything you can think of that would offer the user more control over your object without having to subclass it.
RCIX
Code examples please!!
tuinstoel
The actual class i have for a camera is quite long however ill throw together some dummy code and edit it into the answer.
RCIX
Sometimes you have lots of objects visualized in something like a tree control, and you want different context menus depending on the right-clicked object. I like to put a "Func<object,ContextMenuStrip> MenuProvider" property on such controls.
Wim Coenen
Interesting use!
RCIX
+9  A: 

FlagsAttribute, a small but nice feature when using enum to make a bitmasks:

[Flags]
public enum ConfigOptions
{
    None    = 0,
    A       = 1 << 0,
    B       = 1 << 1,
    Both    = A | B
}

Console.WriteLine( ConfigOptions.A.ToString() );
Console.WriteLine( ConfigOptions.Both.ToString() );
// Will print:
// A
// A, B
+3  A: 

If you have the search textbox in your Visual Studio toolbar, you can type ">of Program.cs" to open the file Program.cs

Kevin
It's a Visual Studio feature, not a C# feature
Thomas Levesque
So are snippets, but they are in the main post under "Visual Studio Features".
Kevin
+7  A: 

The data type can be defined for an enumeration:

enum EnumName : [byte, char, int16, int32, int64, uint16, uint32, uint64]
{
    A = 1,
    B = 2
}
Tolgahan Albayrak
Why would this help us?
tuinstoel
once, i used this feature creating structure database and marshalling
Tolgahan Albayrak
it's not a "hidden feature", it's clearly documented...
Thomas Levesque
most of the features above are documented too..
Tolgahan Albayrak
Most of the hidden features series of S.O. questions are hidden, or rarely used. this is rarely used and easily missed. I would have imagined it was possible to do it, but not known the syntax nor expected it would support unsigned values.
Maslow
+4  A: 

A few from me - make of them what you will.

The attribute:

[assembly::InternalsVisibleTo("SomeAssembly")]

Allows you to expose out the internal methods/properties or data from your assembly to another assembly called 'SomeAssembly'. All protected/private stuff remains hidden.


Static constructors ( otherwise called 'Type Constructor' )

public MyClass
{
  public static MyClass()
  {
     // type init goes here
  }
  ......
}


The keyword internal. So useful in so many ways.

zebrabox
+7  A: 

Advanced Debugging

Display

The already mentioned attributes DebuggerDisplay and DebuggerBrowsable control the visibility of elements and the textual value displayed. Simply overriding ToString() will cause the debugger to use the output of that method.

If you want more complex output you can use/create a Debugger Visualizer, several examples are available here.

Son Of Strike

Microsoft provide a debugger extension known as SOS. This is an extremely powerful (though often confusing) extension which is an excellent way to diagnose 'leaks', more accurately unwanted references to objects no longer required.

Symbol Server for framework source

Following these instructions will allow you to step through the source of some parts of the framework.

Changes in 2010

Several enhancements and new features exist in Visual Studio 2010:

ShuggyCoUk
+27  A: 

Use "throw;" instead of "throw ex;" to preserve stack trace

If re-throwing an exception without adding additional information, use "throw" instead of "throw ex". An empty "throw" statement in a catch block will emit specific IL that re-throws the exception while preserving the original stack trace. "throw ex" loses the stack trace to the original source of the exception.

JoshL
+3  A: 

Ability to create instance of the type based on the generic parameter like this

new T();

epitka
Requires the "where T : new()" constraint.
Simon Svensson
+4  A: 

The Yield keyword is often overlooked when it has a lot of power. I blogged about it awhile ago and discussed benefits (differed processing) and happens under the hood of yield to help give a stronger understanding.

Using Yield in C#

statenjason
+5  A: 

You can create delegates from extension methods as if they were regular methods, currying the this parameter. For example,

static class FunnyExtension {
 public static string Double(this string str) { return str + str; }
 public static int Double(this int num) { return num + num; }
}


Func<string> aaMaker = "a".Double;
Func<string, string> doubler = FunnyExtension.Double;

Console.WriteLine(aaMaker());  //Prints "aa"
Console.WriteLine(doubler("b")); //Prints "bb"

Note that this won't work on extension methods that extend a value type; see this question.

SLaks
+10  A: 

Not sure if this one got mentioned yet but the ThreadStatic attribute is a realy useful one. This makes a static field static just for the current thread.

[ThreadStatic]
private static int _ThreadStaticInteger;

You should not include an initializer because it only get executed once for the entire application, you're better off making the field nullable and checking if the value is null before you use it.

And one more thing for ASP.NET applications threads are reused so if you modify the value it could end up being used for another page request.

Still I have found this useful on several occasions. For example in creating a custom transaction class that:

using (DbTransaction tran = new DbTransaction())
{
    DoQuery("...");
    DoQuery("...");    
}

The DbTransaction constructor sets a ThreadStatic field to its self and resets it to null in the dispose method. DoQuery checks the static field and if != null uses the current transaction if not it defaults to something else. We avoid having to pass the transaction to each method plus it makes it easy to wrap other methods that were not originaly meant to be used with transaction inside a transaction ...

Just one use :)

devilmaster
+7  A: 
HttpContext.Current.Server.Execute

is great for rendering HTML to strings for AJAX callbacks. You can use this with a component instead of piecing together HTML string snippets. I was able to cut page bloat down a couple of hundred KB with virtually no mess. I used it like this:

Page pageHolder = new Page();
UserControl viewControl = (UserControl)pageHolder.LoadControl(@"MyComponent.ascx");
pageHolder.Controls.Add(viewControl);
StringWriter output = new StringWriter();
HttpContext.Current.Server.Execute(pageHolder, output, false);
return output.ToString();
+1. Good tool for unit / integration testing.
RickNZ
+3  A: 

Marketing events as non-serializable:

[field:NonSerializable]
public event SomeDelegate SomeEvent;
Paul Ruane
+13  A: 

You can add and remove delegates with less typing.

Usual way:

handler += new EventHandler(func);

Less typing way:

handler += func;
Mike
Saw that all the time... just make my hand itch :) Also, if you type += in VS and then [TAB][TAB] to generate an event handler, the delegate type is still inserted... kinda annoying.
chakrit
@Mike, don't you mean `handler += func`` ? You're overriding any current registered handlers, if I'm not mistaken. @chakrit, I think that's Resharper.
ANeves
Yes. Typo on my part. Sorry.
Mike
@sr pt: No it is not a resharper functionality. It is a default function in Visual studio (I even believe it is in there since 2005). Visual studio has more of these auto complete snippets.
Gertjan
@Aneves: Resharper lets you easily remove this extra bit of fluff though.
Matthew Scharley
+4  A: 

Generic constraints:

 //Constructor constraint, T has a default empty constructor
class Node<K,T> where T : new() 
{
}

//Reference\Value Type constraints
//T is a struct
public class MyClass<T> where T : struct 

{...}

//T is a reference type
public class MyClass<T> where T : class 

{...}

public class MyClass<T> where T : SomeBaseClass, ISomeInterface 

{...}
+5  A: 

The Or assignment operator is quite nice. You can write this:

x |= y

instead of this:

x = x | y

This is often practical if you have to a variable or property (x in the example) that starts out as false but you want to change it to the value of some other boolean variable/property only when that other value is true.

Mark Seemann
+9  A: 

Extension methods can be called on null; this will not cause a NullReferenceException to be thrown.

Example application: you can define an alternative for ToString() called ToStringOrEmpty() which will return the empty string when called on null.

Wim Coenen
I was pretty geeked when I discovered this fact as it allows me to write much cleaner code in certain instances on my current project.
Kleinux
+15  A: 

I've read through all seven pages, and I'm missing these:

String.Join

I've seen a lot of for-loops to convert a list of items to a string with separators. It's always a pain to make sure you doin't start with a separator and don't end with a separator. A built-in method makes this easier:

String.Join(",", new String[] { "a", "b", "c"});

TODO in comment

Not really a C# feature, more of a Visual Studio feature. When you start your comment with TODO, it's added to your Visual Studio Task List (View -> Task List. Comments)

// TODO: Implement this!
throw new NotImplementedException();

Extension methods meets Generics

You can combine extension methods with Generics, when you think of the tip earlier in this topic, you can add extensions to specific interfaces

public static void Process<T>(this T item) where T:ITest,ITest2 {}

Enumerable.Range

Just want a list of integers?

Enumerable.Range(0, 15)

I'll try to think of some more...

Zyphrax
it is indeed a VS tip, but besides TODO, we also use: QUESTION, HACK, BUG, FIX, REFACTOR, RESOURCE: (with the url from where you got a tip/code) You can add as many as you want through Tools>Options>Task ListAnd with a CI like Hudson that picks these up it's great!
Cohen
In fact, you can add an extension method to *everything* with the generic extension methods...
RCIX
RCIX, that's right but what would be the use of Generics? You could just define an extension method for object.
Zyphrax
`String.Join` and `Enumerable.Range` remind me of Python's equivalents:`",".join(["a", "b", "c"])` and `range(0, 15)`.
Ray Vega
The `Enumerable.Range` can be used as an alternative to a `for` loop. Instead of doing this `for(i = 0; i < 15; i++)`, you can do this `foreach (int i in Enumerable.Range(0, 15))`.
Ray Vega
+6  A: 

Pointers in C#.

They can be used to do in-place string manipulation. This is an unsafe feature so the unsafe keyword is used to mark the region of unsafe code. Also note how the fixed keyword is used to indicate that the memory pointed to is pinned and cannot be moved by the GC. This is essential a pointers point to memory addresses and the GC can move the memory to different address otherwise resulting in an invalid pointer.

 string str = "some string";
 Console.WriteLine(str);
 unsafe
 {
  fixed(char *s = str)
  {
   char *c = s;
   while(*c != '\0')
   {
    *c = Char.ToUpper(*c++);     
   }
  }
 }
 Console.WriteLine(str);

I wouldn't ever do it but just for the sake of this question to demonstrate this feature.

Pratik
if you have to use the unsafe keyword you're doing it wrong! :P
Slace
Not necessarily...using unsafe code may improve performance significantly see "http://stackoverflow.com/questions/541331/effective-way-of-making-negative-of-image-without-external-dlls"
SDX2000
+5  A: 

I like to use the using directive to rename some classes for easy reading like this:

// defines a descriptive name for a complexed data type
using MyDomainClassList = System.Collections.Generic.List<
  MyProjectNameSpace.MyDomainClass>;

....


MyDomainClassList myList = new MyDomainClassList();
/* instead of 
List<MyDomainClass> myList = new List<MyDomainClass>();
*/

This is also very handy for code maintenance. If you need to change the class name, there is only one place you need to change. Another example:

using FloatValue  = float; // you only need to change it once to decimal, double...

....
FloatValue val1;
...
David.Chu.ca
+2  A: 

Exception Filters. So "hidden" you can't even use them (at least from C#) without a post-compilation patch ;)

RandomNickName42
+3  A: 

How about Expression Trees? They are the heart of LINQ and allow for defered execution:

Taken from David Hayden's blog:

In C# 3.0, you can define a delegate as follows using a lambda expression:

Func<int,int> f = x => x + 1;

This delegate is compiled into executable code in your application and can be called as such:

var three = f(2); // 2 + 1

The code works as you would expect. Nothing fancy here.

Expression Trees

When you define the delegate as an Expression Tree by using System.Query.Expression:

Expression<Func<int,int>> expression = x => x + 1;

The delegate is no longer compiled into executable code, but compiled as data that can be converted and compiled into the original delegate.

To actually use the delegate represented as an Expression Tree in your application, you would have to compile and invoke it in your application:

var originalDelegate = expression.Compile();

var three = originalDelegate.Invoke(2);
Dan Diplo
+6  A: 
[field: NonSerialized]
public event EventHandler Event;

This way, the event listener is not serialized.

Just [NonSerialized] does not work, because NonSerializedAttribute can only be applied to fields.

Martin Konicek
+5  A: 

I especially like the nullable DateTime. So if you have some cases where a Date is given and other cases where no Date is given I think this is best to use and IMHO easier to understand as using DateTime.MinValue or anything else...

DateTime? myDate = null;

if (myDate.HasValue)
{
    //doSomething
}
else
{
    //soSomethingElse
}
You can replace .HasValue for == null. Comparing a nullable which is set to null with myVar == null will give you a true.
Gertjan
+3  A: 

If you are trying to create a comma delimited string from a list of items:

string[] itemList = { "Example 1", "Example 2", "Example 3" };
CommaDelimitedStringCollection commaStr = new CommaDelimitedStringCollection();
commaStr.AddRange(itemList);
//outputs Example 1,Example 2,Example 3

Look here for another example.

Brad
I would do string.Join(",", itemList)
Theo
+5  A: 

Zero parameter Lambdas

()=>Console.ReadLine()
pb
Needs explanation of where you'd use this.
Kyralessa
As an argument to any method that takes an Action delegate parameter. In C# 2.0 you would use delegate { Console.ReadLine() } instead.
Christian Hayter
Also useful when coupled with Expression trees parsing :)
chakrit
+4  A: 

I didn't see this:

for (;;);

The same as

while (true) ;
ccook
That has been valid syntax since the dawn of C. :-)
Christian Hayter
+11  A: 

I'm becoming a big fan of extension methods since they can add much wanted functionality to existing code or code you can't edit. One of my favorites I add in to everything I do now is for string.IsNullOrEmpty()

public static class Strings
{
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }
}

This lets you shorten your code a bit like this

var input = Console.ReadLine();
if (input.IsNullOrEmpty())
{
    Console.WriteLine("try again");
}
Brian Surowiec
I hope you realize that this example is impossible, because IsNullOrEmpty is already a member of the type string. Extension methods can not have the same name as any member (static or non-static) of that type, which it extends.
John Leidegren
I'm using this in 2 projects for work right now plus numerous personal projects. I put the IsNullOrEmpty method inside of a class called Strings along with my other extension methods for the string class. I haven't had any issue with the compiler telling me the method name is invalid and have been using it in my code for 3 or 4 months now.
Brian Surowiec
@John: I tried Brian's code, it works.
redtuna
@Brian: What happens, when input is null? I´m not so familiar with extension methods.
Jehof
@John: IsNullOrEmpty is a static method, so it doesn't interfere with dynamic methods.@Jehof: It works because an extension method is just some syntax sugar around a static method call.DynamicFoo.MyMethod() is run as if you wrote MyMethod(DynamicFoo), passing null to a method is perfectly okay.
Jauco
It's nice but maybe confusing, no ? When you read code you don't know it's an extension method. And at first sight it looks strange to do `input.IsNullOrEmpty()`
Julien N
@John I think it works because the original IsNullOrEmpty take one parameter and not this one. So it's just overloading.
Julien N
@Julien N - Ah, right you are. I do write this kind of code, but I just call it IsEmpty and I have another IsNonEmpty, I personally don't like the ! operator much.
John Leidegren
+34  A: 

Conditional string.Format:

Applies different formatting to a number depending on whether the number is positive, negative, or zero.

string s = string.Format("{0:positive;negative;zero}", i);

e.g.

string format = "000;-#;(0)";

string pos = 1.ToString(format);     // 001
string neg = (-1).ToString(format);  // -1
string zer = 0.ToString(format);     // (0)
Christian Hubmann
This is similar to reg expressions, very useful, but I can't remember them either. I handle stuff like above with padleft and padright.
tuinstoel
Cool, I never knew it was possible... Is it documented anywhere ?
Thomas Levesque
@Thomas: It's documented in MSDN in the "The ";" Section Separator" section towards the end of the Custom Numeric Formatting topic at: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
devstuff
Thanks ! Too bad there isn't something similar for bools ;)
Thomas Levesque
+3  A: 

A lot of this is explained already, in the standard. It's a good read for any beginner as well as expert, it's a lot to read, but it's the official standard, and it's filled with juicy details.

Once you fully understand C#, it's time to take this further to understand the fundamentals of the Common Language Infrastructure. The architecture and underpinnings of C#.

I've met a variety of programmers that don't know the difference between an object and a ValueType except the adherent limitations thereof.

Familiarize yourself with these two documents and you'll never become that person.

John Leidegren
+1  A: 

When you need to (a)synchronously communicate between objects about occurance of an event there is special purpose interface called ISynchronizeInvoke.

Quoting MSDN article (link):

Objects that implement this interface can receive notification that an event has occurred, and they can respond to queries about the event. In this way, clients can ensure that one request has been processed before they submit a subsequent request that depends on completion of the first.

Here is a generic wrapper:

protected void OnEvent<T>(EventHandler<T> eventHandler, T args) where T : EventArgs
{
    if (eventHandler == null) return;

    foreach (EventHandler<T> singleEvent in eventHandler.GetInvocationList())
    {
        if (singleEvent.Target != null && singleEvent.Target is ISynchronizeInvoke)
        {
            var target = (ISynchronizeInvoke)singleEvent.Target;

            if (target.InvokeRequired) {
                target.BeginInvoke(singleEvent, new object[] { this, args });
                continue;
            }
        }
        singleEvent(this, args);
    }
}

and here is an example usage:

public event EventHandler<ProgressEventArgs> ProgressChanged;

private void OnProgressChanged(int processed, int total)
{
    OnEvent(ProgressChanged, new ProgressEventArgs(processed, total));
}
Ray
+7  A: 

Expression to initialize a Dictionary in C# 3.5:

new Dictionary<string, Int64>() {{"Testing", 123}, {"Test", 125}};

JoshL
+7  A: 

I don't think someone has mentioned that appending ? after a value type name will make it nullable.

You can do:

DateTime? date = null;

DateTime is a structure.

cornerback84
`Nullable<T>` is not a hidden feature...
Thomas Levesque
Neither is most of these... The point of this question is to show things that most c# devs may not know. I would definitely upvote this since it's not something i think most people know about. (the adding ? to get a nullable, not the nullable itself)
RCIX
+20  A: 

Programmers moving from C/C++ may miss this one:

In C#, % (modulus operator) works on floats!

Jason Williams
+4  A: 

Generics and the Curiously-Recurring Template Pattern really help with some static method/property declarations.

Suppose you are building a class hierarchy:

class Base
{
}

class Foo: Base
{
}

class Bar: Base
{
}

Now, you want to declare static methods on your types that should take parameters (or return values) of the same type or static properties of the same type. For example, you want:

class Base
{
    public static Base Get()
    {
        // Return a suitable Base.
    }
}

class Foo: Base
{
    public static Foo Get()
    {
        // Return a suitable Foo.
    }
}

class Bar: Base
{
    public static Bar Get()
    {
        // Return a suitable Bar.
    }
}

If these static methods basically all do the same thing, then you have lots of duplicated code on your hands. One solution would be to drop type safety on the return values and to always return type Base. However, if you want type safety, then the solution is to declare the Base as:

class Base<T> where T: Base<T>
{
    public static T Get<T>()
    {
        // Return a suitable T.
    }
}

and you Foo and Bar as:

class Foo: Base<Foo>
{
}

class Bar: Base<Bar>
{
}

This way, they will automatically get their copies of the static methods.

This also works wonders to encapsulate the Singleton pattern in a base class (I know the code below is not thread-safe, it just to demonstrate a point):

public class Singleton<T> where T: Singleton<T>, new()
{
  public static T Instance { get; private set; }

  static Singleton<T>()
  {
    Instance = new T();
  }
}

I realize that this forces you to have a public parameterless constructor on your singleton subclass but there is no way to avoid that at compile time without a where T: protected new() construct; however one can use reflection to invoke the protected/private parameterless constructor of the sub-class at runtime to achieve that.

paracycle
I think reflection is the best option here... Performance is not really important in that case, because the constructor will only be called once, and having a public constructor on a singleton type is just plain wrong.
Thomas Levesque
Oh yes, I totally agree. Reflection for protected constructors in the Singleton base class is what I have working in my code. However, I didn't what to pollute the example with all the reflection code.
paracycle
Yup, that's a good example of why you want to use the "infinite interface" as i call it
RCIX
Be aware that any class derived from Foo or Bar will have a static Get that returns either Foo or Bar and not the type of the derived class you're calling upon, whether the derived class is passing its own type to Foo (or Bar respectively) or not. In short: the recurrance only goes one level deep which is not very intuitive to anyone who builds upon your classes.
Sandor Drieënhuizen
Why not just `protected abstract T CreateInstance()` forcing the child class to override that and then in `Instance` property you'd call the method if there's Instance == null ?
chakrit
It's answers to questions like this that makes me wish I could "Favorite" an answer.
Pretzel
@Pretzel - I agree that a proper "Favorite" option would be much better, but I have managed to use up-votes to achieve something similar. You can always up-vote the answer you like and when you later go over the stuff that you have up-voted, you end up finding your "Favorite"s.
paracycle
@Paracycle: Good point. +1 for a good answer and as well as a good answer to a comment. :-)
Pretzel
+7  A: 

Four switch oddities by Eric Lippert

yapiskan
The C# switch statement is weird.
tuinstoel
+3  A: 

Keeps DataGridView from showing the property:

[System.ComponentModel.Browsable(false)]
public String LastActionID{get; private set;}

Lets you set a friendly display for components (like a DataGrid or DataGridView):

[System.ComponentModel.DisplayName("Last Action")]
public String LastAction{get; private set;}

For your backing variables, if you don't want anything accessing them directly this makes it tougher:

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
 private DataController p_dataSources;
Maslow
+6  A: 

The ability to use LINQ to do inline work on collections that used to take iteration and conditionals can be incredibly valuable. It's worth learning how all the LINQ extension methods can help make your code much more compact and maintainable.

Minta szerződés
+1 because i don't see why this got 2 downvotes
RCIX
+5  A: 

One that I just learned recently is that you can still call methods on a nullable value....

It turns out what when you have a nullable value:

decimal? MyValue = null;

where you might think you would have to write:

MyValue == null ? null : MyValue .ToString()

you can instead write:

MyValue.ToString()

I've been aware that I could call MyValue.HasValue and MyValue.Value...but it didn't fully click that I could call ToString().

mezoid
+5  A: 

This will not compile:

namespace ns
{
    class Class1
    {
        Nullable<int> a;
    }
}

The type or namespace name 'Nullable' could not be found (are you missing a using directive or an assembly reference?) <-- missing 'using System;'

But

namespace ns
{
    class Class1
    {
        int? a;
    }
}

will compile! (.NET 2.0).

najmeddine
belongs in strangest corner cases (http://stackoverflow.com/questions/194484/whats-the-strangest-corner-case-youve-seen-in-c-or-net) but +1 anyway
RCIX
Maybe it will directly convert int? to System.Nullable<System.Int32> so that is the reason it works fine?
devoured elysium
+10  A: 

Nested classes can access private members of a outer class.

public class Outer
{
    private int Value { get; set; }

    public class Inner
    {
        protected void ModifyOuterMember(Outer outer, int value)
        {
            outer.Value = value;
        }
    }
}

And now together with the above feature you can also inherit from nested classes as if they were top level classes as shown below.

public class Cheater : Outer.Inner
{
    protected void MakeValue5(Outer outer)
    {
        ModifyOuterMember(outer, 5);
    }
}

These features allow for some interesting possibilities as far as providing access to particular members via somewhat hidden classes.

jpierson
That's almost scary...
RCIX
+18  A: 

JavaScript-like anonymous inline-functions

Return a String:

var s = new Func<String>(() =>
{
    return "Hello World!";
})();

Return a more complex Object:

var d = new Func<Dictionary<Int32, String>>(() =>
{
    var _d = new Dictionary<Int32, String>();

    _d.Add(0, "Hello World!");

    return _d;
})();

A real-world use-case:

var tr = new TableRow();

tr.Cells.AddRange
(
    new[]
    {
        new TableCell { Text = "" },
        new TableCell { Text = "" },
        new TableCell { Text = "" },

        new TableCell
        {
            Text = new Func<String>(() =>
            {
                return @"Result of a chunk of logic, without having to define
                         the logic outside of the TableCell constructor";
            })()
        },

        new TableCell { Text = "" },
        new TableCell { Text = "" }
    }
);

Note: You cannot re-use variable names inside the inline-function's scope.

roosteronacid
+3  A: 

At first - DebuggerTypeProxy.

[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
    private const string TestString = 
        "This should not appear in the debug window.";

    internal class HashtableDebugView
    {
        private Hashtable hashtable;
        public const string TestStringProxy = 
            "This should appear in the debug window.";

        // The constructor for the type proxy class must have a 
        // constructor that takes the target type as a parameter.
        public HashtableDebugView(Hashtable hashtable)
        {
            this.hashtable = hashtable;
        }
    }
}

At second:

ICustomTypeDescriptor

madcyree
+5  A: 

I apologize if this one has been mentioned, but I use this a lot.

An add-in for Visual Studio was developed by Alex Papadimoulis. It's used for pasting regular text as string, string builder, comment or region.

http://weblogs.asp.net/alex%5Fpapadimoulis/archive/2004/05/25/Smart-Paster-1.1-Add-In---StringBuilder-and-Better-C%5F2300%5F-Handling.aspx

In this plugin (I also don't know if this has been mentioned) I noticed that strings are pasted with the string literal prefix:

@

I knew about these, but I didn't know about using a double quote within a literal to escape the quote.

For example

string s = "A line of text" + Environment.NewLine + "Another with a \"quote\"!!";

can be expressed as

string s = @"A line of text 
Another with a ""quote""!!";
fauxtrot
I'm sure it's been posted before and it's not exactly a hidden feature, but I didn't know about the newline this way.
horsedrowner
+11  A: 

When defining custom attributes you can use them with [MyAttAttribute] or with [MyAtt]. When classes exist for both writings, then a compilation error occures.

The @ special character can be used to distinguish between them:

[AttributeUsage(AttributeTargets.All)]
public class X: Attribute
{}

[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}

[X]      // Error: ambiguity
class Class1 {}

[XAttribute]   // Refers to XAttribute
class Class2 {}

[@X]      // Refers to X
class Class3 {}

[@XAttribute]   // Refers to XAttribute
class Class4 {}
codymanix
Of course, the best solution is to fix one or both names so they don't clash in the first place. But this is good to know in case of dealing with third-party libraries that did it wrong, and that you can't fix.
Kyralessa
@Kyralessa: +1, good point...plus I've read this far so someone besides me should benefit from my perseverance!!!
dboarman
+23  A: 

You can "use" multiple objects in one using statement.

using (Font f1= new Font("Arial", 10.0f), f2 = new Font("Arial", 10.0f))
{
    // Use f1 and f2.
}

Note that there is already an answer stating that you can do this:

using (Font f1= new Font("Arial", 10.0f))
using (Font f2 = new Font("Arial", 10.0f))
{    }

Which is different from mine.

codymanix
Nice! Had no idea you could do that. I like it much better than the second one you mentioned.
John
+2  A: 

Not sure Microsoft would like this question, especially with so many responses. I'm sure I once heard a Microsoft head say:

a hidden feature is a wasted feature

... or something to that effect.

Oliver
Hidden features are found by those that care about the fine details.
Sir Psycho
+7  A: 

You can limit the life and thus scope of variables by using { } brackets.

string test = "1";

{ string test2 = "3"; Console.Write(test2); }

Console.Write(test2);

test2 only lives within the brackets.

Nissan Fan
This is true of C++ too.
ChrisF
+16  A: 

Apologies for posting so late, I am new to Stack Overflow so missed the earlier opportunity.

I find that EventHandler<T> is a great feature of the framework that is underutilised.

Most C# developers I come across still define a custom event handler delegate when they are defining custom events, which is simply not necessary anymore.

Instead of:

public delegate void MyCustomEventHandler(object sender, MyCustomEventArgs e);

public class MyCustomEventClass 
{
    public event MyCustomEventHandler MyCustomEvent;
}

you can go:

public class MyCustomEventClass 
{
    public event EventHandler<MyCustomEventArgs> MyCustomEvent;
}

which is a lot more concise, plus you don't get into the dilemma of whether to put the delegate in the .cs file for the class that contains the event, or the EventArgs derived class.

Riko
Here's a link to a snippet that makes it easy to create a custom EventArgs classes and an event that uses it: http://stackoverflow.com/questions/1157072/how-wrong-is-it-to-create-an-event-handler-delegate-with-out-the-standard-obj-se/1157098#1157098
Kyralessa
+4  A: 

Many answers here suggest improvements that Resharper highlights by default, such as changing member variables to readonly or const. There are lots of other code improvements and optimizations they automatically check for, that Visual Studio should have been doing all along.

(R# is the best IMHO tool for .NET, and no, I don't work for them.)

Yurik
How is that a feature of C# ?
Thomas Levesque
I simply haven't seen anything as powerful for Java or C++, so in a way, having a very good IDE+tools is a definite plus to the language. IMHO.
Yurik
+4  A: 

Open generics are another handy feature especially when using Inversion of Control:

container.RegisterType(typeof(IRepository<>), typeof(NHibernateRepository<>));
Michael Valenty
What exactly does that do?
Kyralessa
In this case using Unity IoC container, this will tell the container resolve the closed generic concrete class NHibernateRepository<T> for any requested IRepository<T>. For example, var repository = container.Resolve<IRepository<Customer>>(); would return NHibernateRepository<Customer>;
Michael Valenty
+3  A: 

Use of @ before a string that contains escape char. Basically when a physical path is used to assign in a string variable everybody uses '\' where escape character is present in a string.

e.g. string strPath="D:\websites\web1\images\";

But escape characters can be ignored using @ before the string value.

e.g. string strPath=@"D:\websites\web1\images\";

Himadri
+4  A: 

I love abusing the fact that static templated classes don't share their static members.

Here's a threadsafe (at creation time) and cheap substitute to any Dictionary

public static class MyCachedData<T>{
    static readonly CachedData Value;
    static MyCachedData(){
       Value=// Heavy computation, such as baking IL code or doing lots of reflection on a type
    }
}

Cheers, Florian

Florian Doyon
+5  A: 

The following one is not hidden, but it's quite implicit. I don't know whether samples like the following one have been published here, and I can't see are there any benefits (probably there are none), but I'll try to show a "weird" code. The following sample simulates for statement via functors in C# (delegates / anonymous delegates [lambdas]) and closures. Other flow statements like if, if/else, while and do/whle are simulated as well, but I'm not sure for switch (perhaps, I'm too lazy :)). I've compacted the sample source code a little to make it more clear.

private static readonly Action EmptyAction = () => { };
private static readonly Func<bool> EmptyCondition = () => { return true; };

private sealed class BreakStatementException : Exception { }
private sealed class ContinueStatementException : Exception { }
private static void Break() { throw new BreakStatementException(); }
private static void Continue() { throw new ContinueStatementException(); }

private static void For(Action init, Func<bool> condition, Action postBlock, Action statement) {
    init = init ?? EmptyAction;
    condition = condition ?? EmptyCondition;
    postBlock = postBlock ?? EmptyAction;
    statement = statement ?? EmptyAction;
    for ( init(); condition(); postBlock() ) {
        try {
            statement();
        } catch ( BreakStatementException ) {
            break;
        } catch ( ContinueStatementException ) {
            continue;
        }
    }
}

private static void Main() {
    int i = 0; // avoiding error "Use of unassigned local variable 'i'" if not using `for` init block
    For(() => i = 0, () => i < 10, () => i++,
        () => {
            if ( i == 5 )
                Continue();
            Console.WriteLine(i);
        }
    );
}

If I'm not wrong, this approach is pretty relative to the functional programming practice. Am I right?

Lyubomyr Shaydariv
Not really. Functional programming practice doesn't just mean "replace everything with functions"... it's *thinking* functionally. You're still writing a for loop, an elaborate one. Most of the time you will use recursive implementation when programming functionally as that will allows you to code *declaratively* instead of *imperatively* I think you missed that point.
chakrit
@charkit, thank you for comment. Yes, I've realized just the same few months ago and I see that I was somewhat wrong asking this question. However, I wrote some scripts for my Foobar2K player using the simple Tagz language, and I've found that Tagz is closer to the functional programming practice than the sample above (if I am right in this case).
Lyubomyr Shaydariv
+3  A: 

I love Delegate and also Linq-To-Object. I don't know whether anybody mentioned it or not. But it's simple.

var AnotherThings = from t in 'A collection of things'
                    where (t.Name.StartWith("Foo"))
                    orderby t.Index descending
                    select (new Func<AnotherThing>(() =>
                    {
                        AnotherThing at = t.CreateAnotherThing();
                        at.Initialize();
                        .
                        .
                        .
                        return at;
                    }))());

The above code snippet demonstrates how to define a method inline and call it in a Linq query.

Mehdi Golchin
This code is complex a little in my opinion. Actually I think almost all of C# developers know these features, but they don't use them in such case because of too complex syntax. I'd rather use LINQ extension methods than language-integrated queries here.
Lyubomyr Shaydariv
Actually that's right.
Mehdi Golchin
Oh, Mehdi, you can simplify your code replacing the lambda statement block with a simple expression: () => s + ....
Lyubomyr Shaydariv
It's a sample.sometimes you want to get collection, element by element,and do some process on them and at last select the result.in this situation,you should define a method and put in front of select term,but the above code demonstrates how to use an anonymous method here. so, i admit that the sample is not logical in this way and can write it without lambda and anonymous method, but it's just a sample. i like this way cause can keep query model instead of using extension methods. Is there a better way?
Mehdi Golchin
Why do you use Func<T> instead of anonymous class declaration? It there any benefit of using the queries inside iterators? By the way, if I get a collection and I want to process it, I'd better do something like the following: var c = GetCollection(); /* or just c = from ... in ... select ... - it doesn't matter */ foreach ( var i in c ) /* foreach statement is more pretty when it doesn't containt syntax mess */. Take a look at your parenthesis and you'll understand what I mean.
Lyubomyr Shaydariv
Is it better????
Mehdi Golchin
Emmm, this code is completely different to the previous one. But... But nevermind.
Lyubomyr Shaydariv
that was my mistake, my sample was not clear to all. thanks
Mehdi Golchin
You can have multiline code in a select by using the extension method instead of query comprehension. `Select(x => { /* code */ return x; }`
chakrit
Yes, I know....
Mehdi Golchin
It's useful in different situations such as `if`, `while` and so on; not just in query comprehension.
Mehdi Golchin
+7  A: 

C# allows you to add property setter methods to concrete types that implement readonly interface properties even though the interface declaration itself has no property setter. For example:

public interface IReadOnlyFoo
{
   object SomeReadOnlyProperty { get; }
}

The concrete class looks like this:

internal class Foo : IReadOnlyFoo
{
   public object SomeReadOnlyProperty { get; internal set; }
}

What's interesting about this is that the Foo class is immutable if you cast it to the IReadOnlyFoo interface:

// Create a Foo instance
Foo foo = new Foo();

// This statement is legal
foo.SomeReadOnlyProperty = 12345;

// Make Foo read only
IReadOnlyFoo readOnlyFoo = foo;

// This statement won't compile
readOnlyFoo.SomeReadOnlyProperty = 54321;
plaureano
What if they cast it back to `Foo`?
ChaosPandion
Good point. I find it useful for most interfaces to have read-only properties. After all, interfaces describe behaviour and you normally (or perhaps 'should only') invoke behaviour through methods and not setting properties.
Steve Dunn
I usually use a protected setter for this purpose. My class has a copy constructor which allows all the values to be set from the object being passed in. I then have a derived class that overrides the "readonly" properties `return new ReadOnlyObject(new WriteableVersion() { Property1 = value, //Set all writeable fields... });`
BenAlabaster
+24  A: 

I just wanted to copy that code without the comments. So, the trick is to simply press the Alt button, and then highlight the rectangle you like.(e. g. below).

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "sel")
        //{
        //    lblCat.Text = e.CommandArgument.ToString();
        //}
    }

In the above code if I want to select :

e.CommandName == "sel"

lblCat.Text = e.Comman

Then I press ALt key and select the rectangle and no need to uncomment the lines.

Check this out.

Himadri
Interesting Visual Studio feature, but the question is about C#.
Fernando
Yes. u r right.
Himadri
+1 There is a section "Visual Studio Features" in this question, where your answer fits perfectly.
Roman Boiko
it is named virtual space.
Behrooz
Also it worked just by hit on Alt. Not need to press and hold Alt.
FFire
It's *not* just Visual Studio feature. you can do this in Microsoft Word Notepad2 etc. etc.
chakrit
Very limited compared to Vim's block selection, though.
Don Reba
awesome! I didn't know about this one
Darcy
VS2010 lets you not only select, but edit text in this fashion - such as adding the word "private" to the beginning of every line simply by alt-selecting the space before the lines and starting to type - thereby correcting a poor practice of leaving off the word on multiple field declarations at the same time.
mattdekrey
@mattdekrey: That's a pretty awesome feature! Thanks!
New Start
+22  A: 

C# + CLR:

  1. Thread.MemoryBarrier: Most people wouldn't have used this and there is some inaccurate information on MSDN. But if you know intricacies then you can do nifty lock-free synchronization.

  2. volatile, Thread.VolatileRead, Thread.VolatileWrite: There are very very few people who gets the use of these and even fewer who understands all the risks they avoid and introduce :).

  3. ThreadStatic variables: There was only one situation in past few years I've found that ThreadStatic variables were absolutely god send and indispensable. When you want to do something for entire call chain, for example, they are very useful.

  4. fixed keyword: It's a hidden weapon when you want to make access to elements of large array almost as fast as C++ (by default C# enforces bound checks that slows down things).

  5. default(typeName) keyword can be used outside of generic class as well. It's useful to create empty copy of struct.

  6. One of the handy feature I use is DataRow[columnName].ToString() always returns non-null value. If value in database was NULL, you get empty string.

  7. Use Debugger object to break automatically when you want developer's attention even if s/he hasn't enabled automatic break on eception:


#if DEBUG  
    if (Debugger.IsAttached)  
        Debugger.Break();  
#endif
  1. You can alias complicated ugly looking generic types so you don't have to copy paste them again and again. Also you can make changes to that type in one place. For example,

    using ComplicatedDictionary = Dictionary<int, Dictionary<string, object>>;
    ComplicatedDictionary myDictionary = new ComplicatedDictionary();
ShitalShah
Sweet, great tips, in the last one you had tag trouble... replace < with < and we will be able to read it :)
Cohen
@Cohen you can edit other's posts
DSO
Wow, 1,2 and 7 is really a hidden feature to me :)
chakrit
+12  A: 

Need to return an empty IEnumerable?

public IEnumerable<T> GetEnumerator(){
  yield break;
}
Will
Already in the BCL `System.Linq.Enumerable.Empty<T>()`
chakrit
@chak Which is a wrapper around `return new T[0];`. There are many ways to do this. This is but just one. But it is an interesting one...
Will
This one is a little opaque for my tastes, I prefer chakrit's solution.
Alex Baranosky
+11  A: 

Type-inference for factory methods

I don't know if this has been posted already (I scanned the first post, couldn't find it).

This is best shown with an example, assuming you have this class (to simulate a tuple), in in an attempt to demonstrate all the language features that make this possible I will go through it step by step.

public class Tuple<V1, V2> : Tuple
{
    public readonly V1 v1;
    public readonly V2 v2;

    public Tuple(V1 v1, V2 v2)
    {
      this.v1 = v1;
      this.v2 = v2;
    }
}

Everyone knows how to create an instance of it, such as:

Tuple<int, string> tup = new Tuple<int, string>(1, "Hello, World!");

Not exactly rocket science, now we can of course change the type declaration of the variable to var, like this:

var tup = new Tuple<int, string>(1, "Hello, World!");

Still well known, to digress a bit here's a static method with type parameters, which everyone should be familiar with:

public static void Create<T1, T2>()
{
    // stuff
}

Calling it is, again common knowledge, done like this:

Create<float, double>();

What most people don't know is that if the arguments to the generic method contains all the types it requires they can be inferred, for example:

public static void Create<T1, T2>(T1 a, T2 b)
{
    // stuff
}

These two calls are identical:

Create<float, string>(1.0f, "test");
Create(1.0f, "test");

Since T1 and T2 is inferred from the arguments you passed. Combining this knowledge with the var keyword, we can by adding a second static class with a static method, such as:

public abstract class Tuple
{
    public static Tuple<V1, V2> Create<V1, V2>(V1 v1, V2 v2)
    {
        return new Tuple<V1, V2>(v1, v2);
    }
}

Achieve this effect:

var tup = Tuple.Create(1, "Hello, World!");

This means that the types of the: variable "tup", the type-parameters of "Create" and the return value of "Create" are all inferred from the types you pass as arguments to Create

The full code looks something like this:

public abstract class Tuple
{
    public static Tuple<V1, V2> Create<V1, V2>(V1 v1, V2 v2)
    {
        return new Tuple<V1, V2>(v1, v2);
    }
}

public class Tuple<V1, V2> : Tuple
{
    public readonly V1 v1;
    public readonly V2 v2;

    public Tuple(V1 v1, V2 v2)
    {
        this.v1 = v1;
        this.v2 = v2;
    }
}

// Example usage:
var tup = Tuple.Create(1, "test");

Which gives you fully type inferred factory methods everywhere!

thr
+4  A: 

I like the EditorBrowsableAttribute. It lets you control whether a method/property is displayed or not in Intellisense. You can set the values to Always, Advanced, or Never.

From MSDN...

Remarks

EditorBrowsableAttribute is a hint to a designer indicating whether a property or method is to be displayed. You can use this type in a visual designer or text editor to determine what is visible to the user. For example, the IntelliSense engine in Visual Studio uses this attribute to determine whether to show a property or method.

In Visual C#, you can control when advanced properties appear in IntelliSense and the Properties Window with the Hide Advanced Members setting under Tools | Options | Text Editor | C#. The corresponding EditorBrowsableState is Advanced.

Brian
Using this attribute is often annoying, though. Use it where you have a member on a class due to the inheritance structure, but the member is intentionally unimplemented. But don't hide members that are just "not recommended for use". If there's any legitimate scenario for using the member, leave it visible. A particularly annoying case is TreeView.Sorted ( http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.sorted.aspx ), where the Sorted property has functionality but is still hidden for some strange reason.
Kyralessa
+18  A: 

Easily determine type with which variable was declared (from my answer):

using System;
using System.Collections.Generic;

static class Program
{
    public static Type GetDeclaredType<T>(T x)
    {
        return typeof(T);
    }

    // Demonstrate how GetDeclaredType works
    static void Main(string[] args)
    {
        IList<string> iList = new List<string>();
        List<string> list = null;

        Console.WriteLine(GetDeclaredType(iList).Name);
        Console.WriteLine(GetDeclaredType(list).Name);
    }
}

Results:

IList`1
List`1

And its name (borrowed from "Get variable name"):

static void Main(string[] args)
{
    Console.WriteLine("Name is '{0}'", GetName(new {args}));
    Console.ReadLine();
}

static string GetName<T>(T item) where T : class
{
    var properties = typeof(T).GetProperties();
    return properties[0].Name;
}

Result: Name is 'args'

Roman Boiko
Actually, not bad. The first look at the sample is misleading. I'll remember this trick. :)
Lyubomyr Shaydariv
Cant you simply write `Console.WriteLine(iList.GetType().Name);`?
acidzombie24
@acidzombie24: You'll get `List'1` as the first result, not `IList'1`. And null-reference exception instead of the second result. `GetType()` returns type of an object, not declared type of variable.
Roman Boiko
Great trick, i completely forgot about that.
acidzombie24
Will that work in C# 2.0 or earlier also? not sure if generic type inference was there before 3.0.
Amby
Generics are there since C# (and .NET) 2.0. Type inference is specific for C# 3.0 and doesn't depend on version of .NET. If you have compiler for C# 3.0 or later version, you can build for target framework .NET 2.0 and it will work. The same is true for the second example, where the anonymous types are used: `new {args}`. They are available since C# 3.0, and this code can be built for .NET 2.0 using C# 3.0 compiler. About anonymous types see also http://csharpindepth.com/Articles/General/BluffersGuide3.aspx
Roman Boiko
+4  A: 

Convert enum values to a string value

Given the enum

enum Country
{
    UnitedKingdom, 
    UnitedStates,
    UnitedArabEmirates,
}

using it:

public static void PrintEnumAsString( Country country )
{
    Console.Writeline( country.ToString() );
}

will print the name of the enum value as a string, e.g. "UnitedKingdom"

Phillip Ngan
It's not a trick or even a hidden feature of C# or .NET at all.
Lyubomyr Shaydariv
+15  A: 

I found that only few developers know about this feature.

If you need a method that works with a value-type variable via some interface (implemented by this value type), it's easy to avoid boxing during the method call.

Example code:

using System;
using System.Collections;

interface IFoo {
    void Foo();
}
struct MyStructure : IFoo {
    public void Foo() {
    }
}
public static class Program {
    static void MethodDoesNotBoxArguments<T>(T t) where T : IFoo {
        t.Foo();
    }
    static void Main(string[] args) {
        MyStructure s = new MyStructure();
        MethodThatDoesNotBoxArguments(s);
    }
}

IL code doesn't contain any box instructions:

.method private hidebysig static void  MethodDoesNotBoxArguments<(IFoo) T>(!!T t) cil managed
{
  // Code size       14 (0xe)
  .maxstack  8
  IL_0000:  ldarga.s   t
  IL_0002:  constrained. !!T
  IL_0008:  callvirt   instance void IFoo::Foo()
  IL_000d:  ret
} // end of method Program::MethodDoesNotBoxArguments

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] valuetype MyStructure s)
  IL_0000:  ldloca.s   s
  IL_0002:  initobj    MyStructure
  IL_0008:  ldloc.0
  IL_0009:  call       void Program::MethodDoesNotBoxArguments<valuetype MyStructure>(!!0)
  IL_000e:  ret
} // end of method Program::Main

See Richter, J. CLR via C#, 2nd edition, chapter 14: Interfaces, section about Generics and Interface Constraints.

See also my answer to another question.

Roman Boiko
and why not just put 'Where T : struct' to avoid boxing...
AWC
@AWC: the task is to pass an instance of some interface into our method. So that we can call methods of this interface on a passed instance. Declarations like `void BoxingMethod(IFoo x)` cause boxing if `x` is a value type. Your example doesn't allow calls of interface methods. Code above allows such calls without boxing.
Roman Boiko
+10  A: 

A couple I can think of:

[field: NonSerialized()]
public EventHandler event SomeEvent;

This prevents the event from being serialised. The 'field:' indicates that the attribute should be applied to the event's backing field.

Another little known feature is overriding the add/remove event handlers:

public event EventHandler SomeEvent
{
    add
    {
        // ...
    }

    remove
    {
        // ...
    }
}
Paul Ruane
+3  A: 

__arglist as well

[DllImport("msvcrt40.dll")]
public static extern int printf(string format, __arglist);

static void Main(string[] args)
{
   printf("Hello %s!\n", __arglist("Bart"));
}
Zac Bowling
poor answer with no explanation?
Amr Elsehemy
+1 for the "secret" google==>http://community.bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx
Behrooz
Added example from Behrooz's link
BlueRaja - Danny Pflughoeft
+6  A: 

Dictionary initializers are always useful for quick hacks and unit tests where you need to hardcode some data.

var dict = new Dictionary<int, string> { { 10, "Hello" }, { 20, "World" } };
Matt Dotson
+4  A: 

The Action and Func delegate helpers in conjunction with lambda methods. I use these for simple patterns that need a delegate to improve readability. For example, a simple caching pattern would be to check if the requested object exists in the cache. If it does exist: return the cached object. If it doesn't exist, generate a new instance, cache the new instance and return the new instance. Rather that write this code 1000 times for each object I may store/retrieve from the cache I can write a simple pattern method like so...

private static T CachePattern<T>(string key, Func<T> create) where T : class
{
    if (cache[key] == null)
    {
     cache.Add(key, create());
    }

    return cache[key] as T;
}

... then I can greatly simplify my cache get/set code by using the following in my custom cache manager

public static IUser CurrentUser
{
    get
    {
     return CachePattern<IUser>("CurrentUserKey", () => repository.NewUpUser());
    }
}

Now simple "everyday" code patterns can be written once and reused much more easily IMHO. I don't have to go write a delegate type and figure out how I want to implement a callback, etc. If I can write it in 10 seconds I'm much less apt. to resort to cutting/pasting simple code patterns whether they be lazy initialization and some other examples shown above...

Dave Jellison
Nice one! We have something similar, but with the TimeSpan and CacheItemPriority as a parameter as well! We are using ASP.NET cache.
Peter Gfader
+3  A: 

The "TODO" property and the tasks list

//TODO: [something] 

Adding that to your code (the spacing is important) throws an item in your task list, and double clicking the item will jump you to the appropriate location in your code.

jvenema
You can also define your own custom tags, I think that most people also use: BUG, FIX, REFACTOR, HACK We also use: - MANUAL for missing documentation hints/changes. - QUESTION for changes or code you want the opinion of the development team before proceeding - RESOURCE if the code is based on a blogpost, article, some document (with the url after it)
Cohen
Actually, it's a Visual Studio feature, C# doesn't take care on such comments or whatever something like this.
Lyubomyr Shaydariv
This question 'supports' Visual Studio features as well.
Arda Xi
+3  A: 

I didn't knew about Generic methods which could help avoid using Method Overloadding. Below are overloaded methods to print int and double numbers.

    private static void printNumbers(int [] intNumbers)
    { 
        foreach(int element in intNumbers)
        {
            Console.WriteLine(element);
        }

    }

    private static void printNumbers(double[] doubleNumbers)
    {
        foreach (double element in doubleNumbers)
        {
            Console.WriteLine(element);
        }
    }

Generic method which help to have one method for both of the above

    private static void printNumbers<E>(E [] Numbers)
    {
        foreach (E element in Numbers)
        {
            Console.WriteLine(element);
        }
    }
Ravia
+3  A: 
  • Attaching ? to a Type to make it nullable, ex: int?
  • "c:\dir" instead of @"C:\dir"
iChaib
did you mean "d:\\dir"?
Behrooz
exactly I mean "d:\\" (that's the correct form), but generally people want to write directly "d:\" but they're obliged to use \\, instead of that they could use verbatim strings
iChaib
+6  A: 

Easier-on-the-eyes / condensed ORM-mapping using LINQ

Consider this table:

[MessageId] INT,
[MessageText] NVARCHAR(MAX)
[MessageDate] DATETIME

... And this structure:

struct Message
{
    Int32 Id;
    String Text;
    DateTime Date;
}



Instead of doing something along the lines of:

List<Message> messages = new List<Message>();

foreach (row in DataTable.Rows)
{
    var message = new Message
    {
        Id = Convert.ToInt32(row["MessageId"]),
        Text = Convert.ToString(row["MessageText"]),
        Date = Convert.ToDateTime(row["MessageDate"])
    };

    messages.Add(message);
}

You can use LINQ and do the same thing with fewer lines of code, and in my opinion; more style. Like so:

var messages = DataTable.AsEnumerable().Select(r => new Message
{
    Id = Convert.ToInt32(r["MessageId"]),
    Text = Convert.ToString(r["MessageText"]),
    Date = Convert.ToDateTime(r["MessageDate"])
}).ToList();

This approach can be nested, just like loops can.

roosteronacid
It's an interesting approach, but I'm not convinced that it's easier on the eyes. It's definitely not easier to understand for the average programmer, in my opinion.
Kyralessa
+1 for greatly improving the readability. I think the new code is much easier on the eyes. Although today the average C# programmer is still programming procedurally, that will change quickly. I've found that when you show most procedural programmers the LINQ version they understand immediately and say something like "wow!" and want to know how they can use it themselves.
Ray Burns
@roosteronacid: I hope you don't mind that I simplified your LINQ code slightly to make it even more readable. If it's a problem, just change it back.
Ray Burns
@Ray Burns: thanks for the revision. It's now even more easy on the eyes :)
roosteronacid
+5  A: 

Having read through all 9 pages of this I felt I had to point out a little unknown feature...

This was held true for .NET 1.1, using compression/decompression on gzipped files, one had to either:

  • Download ICSharpCode.ZipLib
  • Or, reference the Java library into your project and use the Java's in-built library to take advantage of the GZip's compression/decompression methods.

It is underused, that I did not know about, (still use ICSharpCode.ZipLib still, even with .NET 2/3.5) was that it was incorporated into the standard BCL version 2 upwards, in the System.IO.Compression namespace... see the MSDN page "GZipStream Class".

tommieb75
Unless its been rewritten, DeflateStream is larger and over 20% slower than the ICSharpCode equivalent which is the reason I still use it.
mythz
+5  A: 

I find the use of the conditional break function in Visual Studio very useful. I like the way it allows me to set the value to something that, for example can only be met in rare occasions and from there I can examine the code further.

Jamie Keeling
+3  A: 

Don't know if this is a secret per se but I loved the added Enumerable (adds to IEnumerable) class in System.Linq.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx

While the yield keyword is already listed. Iterator blocks are freaking amazing. I used them to build Lists that would be tested to see if they were co-prime. It basically allows you to go though a function that returns values one by one and stop any time.

Oh, I almost forgot the best class in the world when you can't optimize it any more. The BackgroundWorker!!!!

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Buttink
+13  A: 

I didn't find anyone who is using string.Join to join strings using a separator. Everyone keeps writing the same ugly for-loop

var sb = new StringBuilder();
var count = list.Count();
for(int i = 0; i < count; i++)
{
  if (sb.Length > 0) sb.Append(seperator);
  sb.Append(list[i]);
}

return sb.ToString();

instead of

return string.Join(separator, list.ToArray());
Mohammad Tayseer
you forgot `if (sb.Length > 0) sb.Append(seperator);` to remove the preceding separator. You also want to cache any `Count()` functions to save re-evaluations and `string.Join()` is only for arrays. Like many others devs I have my own extension methods which is cleaner that `string.Join()`
mythz
I fixed the errors. My point is that I see the former piece of code more than I find the later
Mohammad Tayseer
+3  A: 

Another way of geting IEnumerable through yield without explicity creating an IEnumerable object

public IEnumerable<Request> SomeMethod(IEnumerable<Request> results)
{
    foreach (Request request in Requests)
       yield return DoSomthing(request);
}
devforall
+4  A: 

I just want to mention (because of the OP metioning where T : struct) that one of the C# compiler gotchas is that

where T : Enum

will NOT compile. It throws the error "Constraint cannot be special class 'System.Enum'".

murki
Though, I bet `where T : System.Enum`, with a captial E will.
John Leidegren
Oh John, I actually meant that System.Enum won't compile. Changed my answer to caps now.
murki
+4  A: 

Collection Initializer inside Object Initializer:

MailMessage mail = new MailMessage {
   To = { new MailAddress("[email protected]"), new MailAddress("[email protected]") },
   Subject = "Password Recovery"
};

You can initialize a whole tree in a single expression.

Max Toro
+3  A: 

I am bit late in this conversation and I would like to contribute the following. It may be a new thing for some developers.

public class User
    {
        private long _UserId;
        private String _Name;
        private String _Password;
        private String _Email;

        // Properties
        public long UserId
        {
            get { return this._UserId; }
            set { this._UserId = value; }
        }

        public String Name
        {
            get { return this._Name; }
            set { this._Name = value; }
        }

        public String Password
        {
            get { return this._Password; }
            set { this._Password = value; }
        }

        public String Email
        {
            get { return this._Email; }
            set { this._Email = value; }
        }

    }

The usual way to declare and initialize it is with a constructor or like following.

User user = new User();
user.UserId = 1;
user.Name = "myname";
etc

But I learned following way to initialize it. I know Visual Basic developers will love it because it's like with operator available only in VB.NET and not in C# that is as follows.

User user = new User()
{
    UserId = 1,
    Name = "myname",
    Email = "[email protected]",
    Password = "mypassword"
};
Mubashar Ahmad
Have a look at the answer right below yours – this has already been posted (more than once, I think).
Konrad Rudolph
:) Yes but its above my answer, but it wasn't in summary so i contributed.
Mubashar Ahmad
csharp 3.5 contains this operator too, and you can use autoperties at examples
Avram
this operator <> with operator ... `With` are wayyy more awesome :)
chakrit
+14  A: 

You can store colors in Enum.

public enum MyEnumColors : uint
{
    Normal          = 0xFF9F9F9F,
    Active          = 0xFF8EA98A,
    Error           = 0xFFFF0000
}
FFire
Yes, but what for? Why not create strongly-typed static class attributes instead? Might just as well abuse strings to store arbitrary blobs of data, instead of using classes, as was done often in BASIC.
Konrad Rudolph
+2  A: 

This trick for calling private methods using Delegate.CreateDelegate is extremely neat.

var subject = new Subject();
var doSomething = (Func<String, String>)
    Delegate.CreateDelegate(typeof(Func<String, String>), subject, "DoSomething");
Console.WriteLine(doSomething("Hello Freggles"));

Here's a context where it's useful

Ruben Bartelink
A: 

I don't know if this is a hidden feature (""). Any string function.

rerun
"".func() also works but what good use can we possibly put it to?
Serhat Özgel
Just a quick shortcut for the non static functions
rerun
I saw some code recently that did pretty much the same thing. ((SomeType)null).SomeMethod(); -- I did a double-take, thought for a few seconds, and then grinned. :-)
Pretzel
@pretezel wait, what? wouldn't that exception unless SomeMethod() was an extension method?
Fowl
+7  A: 

To test if an IEnumerable<T> is empty with LINQ, use:

IEnumerable<T>.Any();

  • At first, I was using (IEnumerable<T>.Count() != 0)...
    • Which unnecessarily causes all items in the IEnumerable<T> to be enumerated.
  • As an improvement to this, I went on to use (IEnumerable<T>.FirstOrDefault() == null)...
    • Which is better...
  • But IEnumerable<T>.Any() is the most succinct and performs the best.
Groky
Why is *enumerable.FirstOrDefault() == null* an improvement?
Jim G.
Because it doesn't potentially cause the entire collection to be enumerated.
Groky
But doesn't Any() quit on the first find too?
Jim G.
Yes, it does. That's why it's the best solution.
Groky
Ah... I see. I reformatted your answer a bit.
Jim G.
+1  A: 

Most of the P/Invoke stuff is a bit strange.

Example of attributes:

[DllImport ("gdi32.dll")] 
[return : MarshalAs(UnmanagedType.I4)]
[StructLayout(LayoutKind.Sequential)]
Dax70
+4  A: 

Empty blocks with braces are allowed.

You can write code like this

{
    service.DoTonsOfWork(args);
}

It's helpful when you want to try something without a using or try... finally that you've already written.

//using(var scope = new TransactionScope)
{
    service.DoTonsOfWork(args);
}
Anthony Mastrean
It's also handy as a visual indicator of other types of nesting; for instance, I often use braces to nest the statements between a `Debug.Indent()` and a `Debug.Unindent`.
Kyralessa
they are useful for limiting the scope of a variable
modosansreves
+3  A: 

One of the most useful features Visual Studio has is "Make object id". It generates an id and "attaches" to the object so wherever you look at the object you will also see the id (regardless of the thread).

While debugging right click on the variable tooltip and there you have it. It also works on watched/autos/locals variables.

Silviu
+9  A: 

Using "~" operator with FlagAttribute and enum
Sometime we would use Flag attribute with enum to perform bitwise manipulation on the enumeration.

 [Flags]
 public enum Colors
 {
    None  = 0,
    Red   = 1,
    Blue  = 2,
    White = 4,
    Black = 8,
    Green = 16,
    All   = 31 //It seems pretty bad...
 }

Notice that, the value of option "All" which in enum is quite strange.
Instead of that we can use "~" operator with flagged enum.

 [Flags]
 public enum Colors
 {
    None  = 0,
    Red   = 1,
    Blue  = 2,
    White = 4,
    Black = 8,
    Green = 16,
    All   = ~0 //much better now. that mean 0xffffffff in default.
 }
Millionbonus
it must be 32 i think not 31, what you say?
Mubashar Ahmad
Millionbonus
+7  A: 

With LINQ it's possible to create new functions based on parameters. That's very nice if you have a tiny function which is exectued very often, but the parameters need some time to calculate.

    public Func<int> RandomGenerator
    {
        get
        {
            var r = new Random();
            return () => { return r.Next(); };
        }
    }

    void SomeFunction()
    {
        var result1 = RandomGenerator();

        var x = RandomGenerator;
        var result2 = x();
    }
Actually, that's not LINQ, but lambda expressions.
gimpf
And if you keep creating Random objects over and over, the quality of seeds will suffer. Much better (also space and time wise) to just allocate a static one to use and define it once.
Rubys
no, thats the interesting point, a random object is just created the first time
+1 Not a feature on itself, but an interesting and ridiculously easy pattern for lazy initialization!
Abel
@user287107 No, that's not true, a new random will be created every time the property is accessed. Two times in your example. However if you held on to the delegate returned from the property only a single random is created even though you can call the delegate multiple times.
Patrik Hägne
+16  A: 

Arbitrary nested scopes { }


1. For finer scoping behaviour

{ anywhere inside members }, { using only braces }, { with no control statement }.

void MyWritingMethod() {

    int sameAge = 35;


    { // scope some work
        string name = "Joe";
        Log.Write(name + sameAge.ToString());
    }


    { // scope some other work
        string name = "Susan";
        Log.Write(name + sameAge.ToString());
    }

    // I'll never mix up Joe and Susan again
}

Inside large, confusing or archaic members (not that they should ever exist, however,) it helps me prevent against using wrong variable names. Scope stuff to finer levels.

2. For code beautification or visual semantics

For example, this XML writing code follows the indentation level of the actual generated XML (i.e. Visual Studio will indent the scoping braces accordingly)

XmlWriter xw = new XmlWriter(..);

//<root>
xw.WriteStartElement("root");
{
    //<game>
    xw.WriteStartElement("game");
    {
        //<score>#</score>
        for (int i = 0; i < scores.Length; ++i) // multiple scores
            xw.WriteElementString("score", scores[i].ToString());

    }
    //</game>
    xw.WriteEndElement();
}
//</root>
xw.WriteEndElement();

3. Mimic a 'with' statement

(Also another use to keep temp work out of the main scope)
Provided by Patrik: sometimes used to mimic the VB "with-statement" in C#.

var somePerson = this.GetPerson();  // whatever 
{ 
    var p = somePerson; 
    p.FirstName = "John"; 
    p.LastName = "Doe"; 
    //... 
    p.City = "Gotham"; 
} 

For the discerning programmer.

John K
This is something I've sometimes used to mimic the VB "with-statement" in C#.var somePerson = this.GetPerson(); // whatever{ var p = somePerson; p.FirstName = "John"; p.LastName = "Doe"; //... p.City = "Gotham";}
Patrik Hägne
Thanks @Patrik. Added your example to the answer.
John K
+1, I have done this before. However it only served to confuse my coworkers. The example with the xml writer is pretty sweet though. I would do a scores.ForEach(...) though to make the indentation right.
Bill Barry
One place I like to use this is between a `Debug.Indent()` and a `Debug.Unindent()`. Gives a good visual in the code of what's happening.
Kyralessa
+6  A: 

You can change rounding scheme using:

var value = -0.5;
var value2 = 0.5;
var value3 = 1.4;

Console.WriteLine( Math.Round(value, MidpointRounding.AwayFromZero) ); //out: -1
Console.WriteLine(Math.Round(value2, MidpointRounding.AwayFromZero)); //out: 1
Console.WriteLine(Math.Round(value3, MidpointRounding.ToEven)); //out: 1
dario
+2  A: 

This relates to static constructors. This is a method for performing static destruction (i.e. cleaning up resources when the program quits).

First off the class:

class StaticDestructor
{
    /// <summary>
    /// The delegate that is invoked when the destructor is called.
    /// </summary>
    public delegate void Handler();
    private Handler doDestroy;

    /// <summary>
    /// Creates a new static destructor with the specified delegate to handle the destruction.
    /// </summary>
    /// <param name="method">The delegate that will handle destruction.</param>
    public StaticDestructor(Handler method)
    {
        doDestroy = method;
    }

    ~StaticDestructor()
    {
        doDestroy();
    }
}

Then as a member of the class you wish to have a "static destructor" do:

private static readonly StaticDestructor destructor = new StaticDestructor
(
    delegate()
    {
        //Cleanup here
    }
);

This will now be called when final garbage collection occurs. This is useful if you absolutely need to free up certain resources.

A quick and dirty program exhibiting this behavior:

using System;

namespace TestStaticDestructor
{
    class StaticDestructor
    {
        public delegate void Handler();
        private Handler doDestroy;

        public StaticDestructor(Handler method)
        {
            doDestroy = method;
        }

        ~StaticDestructor()
        {
            doDestroy();
        }
    }

    class SomeClass
    {
        static SomeClass()
        {
            Console.WriteLine("Statically constructed!");
        }

        static readonly StaticDestructor destructor = new StaticDestructor(
            delegate()
            {
                Console.WriteLine("Statically destructed!");
            }
        );
    }

    class Program
    {
        static void Main(string[] args)
        {
            SomeClass someClass = new SomeClass();
            someClass = null;
            System.Threading.Thread.Sleep(1000);
        }
    }
}

When the program exits, the "static destructor" is called.

Joshua Rodgers
+5  A: 

Nullable.GetValueOrDefault ?

Snake
or the `??` operator for nullable types.
jalexiou
+6  A: 

I like

#if DEBUG
           //Code run in debugging mode

#else
           //Code run in release mode

#endif
Nakul Chaudhary
Is this a hidden feature?
pradeeptp
I wouldn't say it's exactly a hidden feature, but in all the C# books that I've read, it isn't something that's talked about a lot. A nice reminder at the very least.
Pretzel
Although this can be handy it should be used with care. I have seen a couple of cases where deployed code behaved differently from local code.
Ombergen
+3  A: 

With reference to the post w/ perma link "http://stackoverflow.com/questions/9033/hidden-features-of-c/2495330#2495330", there is another way to accomplish the same - indentation / line breaks. Check this out..

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.NewLineOnAttributes = true;
xmlWriterSettings.Indent = true;


XmlWriter xml = XmlWriter.Create(@"C:\file.xml", xmlWriterSettings);

// Start writing the data using xml.WriteStartElement(), xml.WriteElementString(...), xml.WriteEndElement() etc

I am not sure whether this is an unknown feature though!

Karthik
+10  A: 

What about IObservable?

Pretty much everybody knows IEnumerable but their mathematical dual seems to be unknown IObservable. Maybe because its new in .NET 4.

What it does is instead of pulling the information (like an enumerable) it pushes information to the subscriber(s) of the observerable.

Together with the Rx extensions it will change how we deal with events. Just to illustrate how powerful it is check a very short example here.

ntziolis
+10  A: 

When a class implements INotifyPropertyChanged and you want to inform the binding system (WPF, Silverlight, etc.) that multiple bound properties of an object (ViewModel) have changed you can raise the PropertyChanged-Event with null or String.Empty.

This is documented in MSDN, but code examples and articles often don´t explain this possibility. I found it very useful.

public class BoundObject : INotifyPropertyChanged {

    private int _value;
    private string _text;

    public event PropertyChangedEventHandler PropertyChanged;

    public int Value {
        get {
            return _value;
        }
        set {
            if (_value != value) {
                _value = value;
                OnPropertyChanged("Value");
            }
        }
    }

    public string Text {
        get {
            return _text;
        }
        set {
            if (_text != value) {
                _text = value;
                OnPropertyChanged("Text");
            }
        }
    }

    public void Init(){
        _text = "InitialValue";
        _value = 1;
        OnPropertyChanged(string.Empty);
    }

    public void Reset() {
        _text = "DefaultValue";
        _value = 0;
        OnPropertyChanged(string.Empty);
    }

    private void OnPropertyChanged(string propertyName) {
        PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);

        if (PropertyChanged != null) {
            PropertyChanged(this, e);
        }
    }
}
Jehof
+5  A: 

This means T must have a public parameterless constructor :

 class MyClass<T> where T : new()
 {

 }
Navid Farhadi
+4  A: 

I was reading thru the book "Pro ASP.NET MVC Framework" (APress) and observed something the author was doing with a Dictionary object that was foreign to me.

He added a new Key/Value Pair without using the Add() method. He then overwrote that same Key/Value pair without having to check if that key already existed. For example:

Dictionary<string, int> nameAgeDict = new Dictionary<string, int>();
nameAgeDict["Joe"] = 34;      // no error. will just auto-add key/value
nameAgeDict["Joe"] = 41;      // no error. key/value just get overwritten
nameAgeDict.Add("Joe", 30);   // ERROR! key already exists

There are many cases where I don't need to check if my Dictionary already has a key or not and I just want to add the respective key/value pair (overwriting the existing key/value pair, if necessary.) Prior to this discovery, I would always have to check to see if the key already existed before adding it.

Pretzel
+1  A: 

Separate static fields depending on the generic type of the surrounding class.

    public class StaticConstrucEx2Outer<T> {

 // Will hold a different value depending on the specicified generic type
 public T SomeProperty { get; set; }

 static StaticConstrucEx2Outer() {
  Console.WriteLine("StaticConstrucEx2Outer " + typeof(T).Name);
 }

 public class StaticConstrucEx2Inner<U, V> {

  static StaticConstrucEx2Inner() {

   Console.WriteLine("Outer <{0}> : Inner <{1}><{2}>",
    typeof(T).Name,
    typeof(U).Name,
    typeof(V).Name);
  }

  public static void FooBar() {}
 }

 public class SCInner {

  static SCInner() {
   Console.WriteLine("SCInner init <{0}>", typeof(T).Name);
  }

  public static void FooBar() {}
 }
}


StaticConstrucEx2Outer<int>.StaticConstrucEx2Inner<string, DateTime>.FooBar();
StaticConstrucEx2Outer<int>.SCInner.FooBar();

StaticConstrucEx2Outer<string>.StaticConstrucEx2Inner<string, DateTime>.FooBar();
StaticConstrucEx2Outer<string>.SCInner.FooBar();

StaticConstrucEx2Outer<string>.StaticConstrucEx2Inner<string, Int16>.FooBar();
StaticConstrucEx2Outer<string>.SCInner.FooBar();

StaticConstrucEx2Outer<string>.StaticConstrucEx2Inner<string, UInt32>.FooBar();

StaticConstrucEx2Outer<long>.StaticConstrucEx2Inner<string, UInt32>.FooBar();

Will produce the following output

Outer <Int32> : Inner <String><DateTime>
SCInner init <Int32>

Outer <String> : Inner <String><DateTime>
SCInner init <String>

Outer <String> : Inner <String><Int16>

Outer <String> : Inner <String><UInt32>

Outer <Int64> : Inner <String><UInt32>
Sir Psycho
I can only see static constuctors. None of the classes are defined as static...
MPritch
A: 

I would also add writing simple Macros in the IDE to the list.

Johann Strydom
+10  A: 

OK, it may seem obvious, but I want to mention the Object.Equals method (the static one, with two arguments).

I'm pretty sure many people don't even know about it, or forget it's there, yet it can really help in some cases. For instance, when you want to compare two objects for equality, not knowing if they're null. How many times did you write something like that :

if ((x == y) || ((x != null && y != null) && x.Equals(y)))
{
    ...
}

When you can just write :

if (Object.Equals(x, y))
{
    ...
}

(Object.Equals is actually implemented exactly like in the first code sample)

Thomas Levesque
+14  A: 

Width in string.Format()

Console.WriteLine("Product: {0,-7} Price: {1,5}", product1, price1);
Console.WriteLine("Product: {0,-7} Price: {1,5}", product2, price2);

produces

alt text

from Prabir's Blog | Hidden C# feature

Kim Tranjan
+2  A: 

Lately I learned about the String.Join method. It is really useful when building strings like columns to select by a query.

centro
That's in the .NET Framework (not in C# itself) and is far from hidden, actually. ;-)
peSHIr
Well, I'm pretty sure that it is more hidden than the String.IsNullOrEmpty method which is not in C# but in the .NET Framework too. In the summary at the beginning there is a lot of stuff not specific to C# but .NET in general.
centro
Isn't that the same as String.Concat ?
Jack Marchetti
No, it's quite different. For example there's an array of strings and you want to get those strings separated by some character, e.g. by comma like "string1,string2,string3". That's what String.Join is for.
centro
+25  A: 

typedefs

Someone posted that they miss typedefs but you can do it like this

using ListOfDictionary = System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, string>>;

and declare it as

ListOfDictionary list = new ListOfDictionary();
BuildStarted
Just keep in mind that this method is scoped, at best, for the current file. You will need to add this to the top of every file in your project.
Matthew Scharley
It is not a hidden feature. It's completely documented in the specification.
Lyubomyr Shaydariv
@Lyubomyr and so are 90% of answers to this question.
C. Ross
Typedef is a compilation solution and it's looks like this solution is a runtime. The result is clearly not the same in therms of performances ...
Niklaos
Niklaos, checking the Il it's a compile time substitution and not something at runtime.
BuildStarted
+43  A: 

Here is a new method of the string class in C# 4.0:

String.IsNullOrWhiteSpace(String value)

It's about time.

jcruz
What's the problem with making your own util method that returns this: `(myString ?? "").Trim() == ""`
Charlie Somerville
@Charlie Isn't a carriage return treated as whitespace too?
MPritch
Hmm, good point.
Charlie Somerville
I definitely prefer [Haack's approach](http://haacked.com/archive/2010/06/16/null-or-empty-coalescing.aspx) now that I have tried it out. You put together string extensions methods for AsNullIfEmpty and AsNullIfWhiteSpace. By doing it this way, you can then use the result in a coalescing operator: `SomeString.AsNullIfEmpty() ?? "default value"`.
patridge
I forgot that I had to tweak Haack's code to do something like @Charlie has because he uses the new IsNullOrWhiteSpace. I still like being able to consume the result in a `??` operator, though.
patridge
I really don't like "". You should use string.Empty (myString ?? string.Empty).Trim() == string.Empty as it conveys a bit more.
burnt_hand
+12  A: 

Not really hidden, but useful. When you've got an enum with flags, you can use shift-left to make things clearer. e.g.

[Flags]
public enum ErrorTypes {
    None              = 0,
    MissingPassword   = 1 << 0,
    MissingUsername   = 1 << 1,
    PasswordIncorrect = 1 << 2 
}
Steve Dunn
Can you explain this? Not sure I follow how this works or what it does
burnt_hand
It's just shifting the bits in the binary representation of the number, 0001 << 1 becomes 0010,0001 << 2 becomes 0100,0001 << 3 becomes 1000
Chad
Nice one. @burnt_hand it's the shift operator http://msdn.microsoft.com/en-us/library/aa691377%28v=VS.71%29.aspx. (shifts left-hand value left by the right-hand number of bits)
Julien N
(8 bit representation for brevity) `0000 0001` shifted one place left is `0000 00010` which is 2. It's a nice trick but I'm not sure it's clearer than = 1, = 2, = 4 etc unless you know bit shifting. It makes sure the reader has some CS experience though :)
Chris S
Interesting, but anyone who doesn't immediately recognize the pattern 0,1,2,4,8...16384,32768... probably shouldn't be reading the code, and almost certainly won't know that 1 << 2 == 4.
ScottS
FlagsAttribute alone should make one realize that enum is a bit field. IMO shifting bits in enum definition is not for readability but for lazies, who don't bother remembering the power of two values ;)
AOI Karasu
@AOIKarasu- C# itself is for lazies who can't be bothered with lower level languages. Lower level languages are for lazies who can't be bother with Assembler. Assembler is for lazies who can't be bothered typing in binary. Typing is for lazies who can't be bothered etching tiny dots on a hard disk.Hard disks are for lazies who can't be bothered with punch cards. Punch cards are for lazies who can't be bothered scribing on paper. Paper is for lazies who can't be bothered etching on stone tablets. I'm in all of those varieties of lazy! :) Bit 9 is still easer to read as `1 << 9` than 512. YMMV!
Steve Dunn
You're leaving one out, since 1 << 1 is 2, your first flag should be 1 << 0.
Dave Van den Eynde
Well spotted @Dave!
Steve Dunn
@Chad - Perfect explanation. I knew of pushing bits around, but not applying that to Flags. Mine always were the 0,1,2,4,8 style.
burnt_hand
Is this really clearer than 0, 1, 2, 4, 8, 16...?
Kyralessa
+15  A: 

Another note on event handlers: you can simply create a raise extension method like so:

public static class EventExtensions {
    public static void Raise<T>(this EventHandler<T> @event, 
                                object sender, T args) where T : EventArgs {
        if(@event!= null) {
            @event(sender, args);
        }
    }
}

Then you can use it to raise events:

public class MyImportantThing {
    public event EventHandler<MyImportantEventEventArgs> SomethingHappens;
    ...
    public void Bleh() {
        SomethingHappens.Raise(this, new MyImportantEventEventArgs { X=true });
    }
}

This method has the added advantage of enforcing a coding standard (using EventHandler<>).

There isn't a point in writing the same exact function over and over and over again. Perhaps the next version of C# will finally have an InlineAttribute that can be placed on the extension method and will cause the compiler to inline the method definition (which would make this way nearly standard, and the fastest).

edit: removed temp variable inside extension method based on comments

Bill Barry
You don't need the `temp` variable. It's used for avoiding an async change, but the `event` parameter is sufficient for that.
jpbochi
I am not so sure about that. The parameter is a reference type, so @event is the same object as the event in the class (unless there is something going on with the event syntax that I don't know about). Being that the assignment and checking code is recommended practice, I expect that the assignment operator is defined here to get around this problem. Regardless, it doesn't hurt to be safe.
Bill Barry
jpbochi is right, the temp variable does nothing. The parameter is a reference type, that's right but it's not passed by reference and there's a huge difference. Also, this extension works only for events using the the generic EventHandler delegate. When the generic delegate is used it's a neat trick though.
Patrik Hägne
+34  A: 

When debugging, you can type $exception in the Watch\QuickWatch\Immediate window and get all the info on the exception of the current frame. This is very useful if you've got 1st chance exceptions turned on!

Steve Dunn
Had no idea! Sweet. +1
jcollum
+1 Use this all the time, excellent tip!
scraimer
Never knew that now using it regularly.
PeteT
+3  A: 

I don't condone it, but I was surprised that goto is still around ducks incoming projectiles

STW
I use `goto` on occasion. There is nothing wrong with it.
ChaosPandion
@ChaosPandion watch out for Velociraptorshttp://xkcd.com/292/
STW
Since the switch construct in C# no longer allows case fall-through, the goto statement is the only thing that you have to pass control onto another case statement, just like it says in the documentation that you link to. Hidden? Perhaps. Wrong? Absolutely not.
Dave Van den Eynde
@Chaos: Key word: "on occasion".
bobobobo
Of course by "no longer" I didn't mean to imply that C# once did, but merely referenced to its ancestry.
Dave Van den Eynde
Remember that we also need to support codes/algorithms written in the early days, containing goto, or transfer them from C/C++. You don't want to change a bit of such codes, and that is a reason for the existence of the "goto" keyword.
ileon
@ileon a good point; personally my favorite use of `goto` to is knock down C# developers down a notch when they get elitist against VB.NET--I code both, so I have to have ammo that goes both ways :D
STW
`goto` is for those rare circumstances where you can't jump out of a control structure cleanly any other way.
Loadmaster
@Dave Van den Eynde: case fall-through is dangerous, that's why (I suppose) it is not allowed in C#. So I don't think using `goto` in those cases is the best thing to do.
MainMa
Case fall-through is dangerous because it can be easily overlooked when reading code. Explicitly demanding goto is the best thing that happened to C#.
Dave Van den Eynde
+26  A: 

RealProxy lets you create your own proxies for existing types.

This is super-advanced and I haven't seen anyone else use it -- which may mean that it's also really not that useful for most folks -- but it's one of those things that's good to know.

Basically, the .NET RealProxy class lets you create what is called a transparent proxy to another type. Transparent in this case means that it looks completely like the proxied target object to its client -- but it's really not: it's an instance of your class, which is derived from RealProxy.

This lets you apply powerful and comprehensive interception and "intermediation" services between the client and any methods or properties invoked on the real target object. Couple this power with the factory pattern (IoC etc), and you can hand back transparent proxies instead of real objects, allowing you to intercept all calls to the real objects and perform actions before and after each method invocation. In fact, I believe this is the very functionality .NET uses for remoting across app domain, process, and machine boundaries: .NET intercepts all access, sends serialized info to the remote object, receives the response, and returns it to your code.

Maybe an example will make it clear how this can be useful: I created a reference service stack for my last job as enterprise architect which specified the standard internal composition (the "stack") of any new WCF services across the division. The model mandated that the data access layer for (say) the Foo service implement IDAL<Foo>: create a Foo, read a Foo, update a Foo, delete a Foo. Service developers used supplied common code (from me) that would locate and load the required DAL for a service:

IDAL<T> GetDAL<T>(); // retrieve data access layer for entity T

Data access strategies in that company had often been, well, performance-challenged. As an architect, I couldn't watch over every service developer to make sure that he/she wrote a performant data access layer. But what I could do within the GetDAL factory pattern was create a transparent proxy to the requested DAL (once the common service model code located the DLL and loaded it), and use high-performance timing APIs to profile all calls to any method of the DAL. Ranking laggards then is just a matter of sorting DAL call timings by descending total time. The advantage to this over development profiling (e.g. in the IDE) is that it can be done in the production environment as well, to ensure SLAs.

Here is an example of test code I wrote for the "entity profiler," which was common code to create a profiling proxy for any type with a single line:

[Test, Category("ProfileEntity")]
public void MyTest()
{
    // this is the object that we want profiled.
    // we would normally pass this around and call
    // methods on this instance.
    DALToBeProfiled dal = new DALToBeProfiled();

    // To profile, instead we obtain our proxy
    // and pass it around instead.
    DALToBeProfiled dalProxy = (DALToBeProfiled)EntityProfiler.Instance(dal);

    // or...
    DALToBeProfiled dalProxy2 = EntityProfiler<DALToBeProfiled>.Instance(dal);

    // Now use proxy wherever we would have used the original...
    // All methods' timings are automatically recorded
    // with a high-resolution timer
    DoStuffToThisObject(dalProxy);

    // Output profiling results
    ProfileManager.Instance.ToConsole();
}

Again, this lets you intercept all methods and properties called by the client on the target object! In your RealProxy-derived class, you have to override Invoke:

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[SecurityPermission(SecurityAction.LinkDemand, 
    Flags = SecurityPermissionFlag.Infrastructure)] // per FxCop
public override IMessage Invoke(IMessage msg)
{
    IMethodCallMessage msgMethodCall = msg as IMethodCallMessage;
    Debug.Assert(msgMethodCall != null); // should not be null - research Invoke if this trips. KWB 2009.05.28

    // The MethodCallMessageWrapper
    // provides read/write access to the method 
    // call arguments. 
    MethodCallMessageWrapper mc =
        new MethodCallMessageWrapper(msgMethodCall);

    // This is the reflected method base of the called method. 
    MethodInfo mi = (MethodInfo)mc.MethodBase;

    IMessage retval = null;

    // Pass the call to the method and get our return value
    string profileName = ProfileClassName + "." + mi.Name;

    using (ProfileManager.Start(profileName))
    {
        IMessage myReturnMessage =
           RemotingServices.ExecuteMessage(_target, msgMethodCall);

        retval = myReturnMessage;
    }

    return retval;
}

Isn't it fascinating what .NET can do? The only restriction is that the target type must be derived from MarshalByRefObject. I hope this is helpful to someone.

Keith Bluestone
I've used RealProxy before to produce detailed logs of COM interop conversations... Very handy when you want to give a supplier a picture of *exactly* where their component is failing! I only wish it was possible to use this on objects of *any* type...
Shog9
Yes, I agree, and not completely sure why the MarshalByRefObject restriction exists...
Keith Bluestone
Thanks for the detailed explanation! Although this is not really a feature of the C# language.
M4N
Yep, that's what Remoting is based on. And I once tried to use it, to do some Remoting interception. For some easy interception scenarios that can be very nice. Unfortunately, I learned that there are some limitations deep inside and .Net internals are occasionally vicious, so be wary :)
akavel
+12  A: 

Also useful, but not commonly used : Constrained Execution Regions.

A quote from BCL Team blog :

Constrained execution regions (CER's) exist to help a developer write her code to maintain consistency. The CLR doesn't guarantee that the developer's code is correct, but the CLR does hoist all of the runtime-induced failure points (ie, async exceptions) to either before the code runs, or after it has completed. Combined with constraints on what the developer can put in a CER, these are a useful way of making strong guarantees about whether your code will execute. CER's are eagerly prepared, meaning that when we see one, we will eagerly JIT any code found in its statically-discoverable call graph. If the CLR's host cares about stack overflow, we'll probe for some amount of stack space as well (though perhaps not enough stack space for any arbitrary method*). We also delay thread aborts until the CER has finished running.

It can be useful when making edits to more than one field of a data structure in an atomic fashion. So it helps to have transactions on objects.

Also CriticalFinalizerObject seems to be hidden(at least who are not writing unsafe code). A CriticalFinalizerObject guarantees that garbage collection will execute the finalizer. Upon allocation, the finalizer and its call graph are prepared in advance.

Incognito
Hmm... could that make aborting threads safe if they were written for it?
chaiguy
+17  A: 

AppDomain.UnhandledException Event is also candidate for being hidden.

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery. Caution is advised, because program data can become corrupted when exceptions are not handled.

We can see, even on this site, a lot of people are wondering why their application is not starting, why it crashed, etc. The AppDomain.UnhandledException event can be very useful for such cases as it provides the possibility at least to log the reason of application failure.

Incognito
There is more to know about: http://stackoverflow.com/questions/219594/net-whats-the-best-way-to-implement-a-catch-all-exceptions-handler
modosansreves
+15  A: 
string.Empty

I know it's not fantastical (ludicrously odd), but I use it all the time instead of "".

And it's pretty well hidden until someone tells you it's there.

burnt_hand
Isn't "" shorter than string.Empty?
Arnis L.
@Arnis: `string.Empty` is more 'correct'. For reference though, `.Empty` isn't exactly a sparsely used value, most value types implement it, and Microsoft's guidelines encourage it's use (and indeed, static versions of common values, `MinValue` and `MaxValue` being two other common values). It can prevent unnecessary creation of duplicate value types when the same instance can be used (immutable value types).
Matthew Scharley
@Arnos: It is not about beying short; it is about maintainability: understanding the meaning of something 2 months after you wrote some code. In stead of wondering where "" actually meant " " (and forgotting a space) or an empty string. `String.Empty` is much clearer. This holds for the `.Empty` pattern in general: you see it in many more types.
Jeroen Pluimers
Not exactly hidden if you know a little bit about the framework.
Dave Van den Eynde
This is one of things you get religious arguments about: `string.Empty` vs `""` - personally I find the latter much easier to read, and it makes no different to the compiled code.
Keith
As close to hidden as anything with signs pointing to it. For instance, StyleCop will tell you to use it:Warning 2 SA1122: Use string.Empty rather than "".
Task
I always found it a bit weird to be able to do `typename.property` in C#
bobobobo
Microsoft says so? Oh... then i must be wrong. I'm sorry. ;)
Arnis L.
bobobobo: static properties weird?
Dave Van den Eynde
I find http://blogs.msdn.com/b/ericlippert/archive/2009/09/28/string-interning-and-string-empty.aspx to be of interest. It points out a case where `""` and `string.Empty` will not have the same behavior during `==` comparisons.
Brian
@bobobobo: I always use the upper case type names if accessing a type property or method: String.Empty instead of string.Empty or Double.Parse("1") instead of double.Parse("1"). I know it makes no difference for .net but it is more for myself to express that I use a value type (or string) like an object. In Java double and Double is not the same which is a little more consistent as in .net but I like the .net value type and string handling more. I got fooled more than once in Java by doing String a = "1"; String b = "1"; if(a == b) {...};
SchlaWiener
String.Empty is .NET, not C#. It is accessible from any syntax on the .NET framework.
John K
@Jeroen Pluimers - That's exactly why I use it
burnt_hand
+11  A: 

fixed statement

This statement prevents the garbage collector from relocating a movable variable. Fixed can also be used to create fixed size buffers.

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of statement.

stackalloc

The stackalloc allocates a block of memory on the stack.

Incognito
I think this should include an example of why/how this can be useful
Neil N
@Neil You can follow the links in the answer. There you can see some examples and more details on remarks section. Tell me if that will not be satisfying I'll update the answer with example code.
Incognito
Code formatting and links don't mix well; it's not immediately obvious that the keywords are links as well. But of course, the links are relevant, once you find them :-)
Zano
@Zano code formating is removed. Hope it is easy to notice links now.
Incognito
+10  A: 

You can put several attributes in one pair of square brackets:

    [OperationContract, ServiceKnownType(typeof(Prism)), ServiceKnownType(typeof(Cuboid))]
    Shape GetShape();
Ozan
+4  A: 

Not sure if this one has been mentioned or not (11 pages!!)

But the OptionalField attribute for classes is amazing when you are versioning classes/objects that are going to be serialized.

http://msdn.microsoft.com/en-us/library/ms229752(VS.80).aspx

WedTM
+2  A: 

Accessing local variables from anonymous methods allows you to wrap just about any code with new control flow logic, without having to factor out that code into another method. Local variables declared outside the method are available inside the method such as the endOfLineChar local variable in the example here:

http://aaronls.wordpress.com/2010/02/02/retrying-on-exception-conditionally/

AaronLS