Hidden Features of C#? [closed]
Asked Answered
C

176

1473

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

Attributes

Syntax

Language Features

Visual Studio Features

Framework

Methods and Properties

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

Carlock answered 12/8, 2008 at 16:32 Comment(0)
E
751

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;
Eyot answered 12/8, 2008 at 16:32 Comment(0)
Y
583

lambdas and type inference are underrated. 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 :-)

Yearwood answered 12/8, 2008 at 16:32 Comment(0)
S
527

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;
Stubbs answered 12/8, 2008 at 16:32 Comment(0)
S
453

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.

Schrick answered 12/8, 2008 at 16:32 Comment(0)
S
437

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.

Stubbs answered 12/8, 2008 at 16:32 Comment(15)
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.Leptosome
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.Dilworth
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 ms Invariant Match Test: Got 100000 matches in 23 ms string.Equals Match Test: Got 100000 matches in 8 msThralldom
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).Wharfinger
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.Toein
@neilwhitaker1: That is not an issue when using invariant conversions.Nutty
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.Nutty
Don't question the mighty Jeffrey Richter! :P Although for probably 90% of applications by SO developers this will make a tiny difference.Fluor
ToUpperInvariant is preferred because it makes all characters round-trip. See msdn.microsoft.com/en-us/library/bb386042.aspx. For comparisons, write"a".Equals("A", StringComparison.OrdinalIgnoreCase)Urien
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.Gareri
@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.Pella
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.Ciel
FYI - If you're doing string comparisons for security purposes consider reading this post: leastprivilege.com/…Boss
The way I read the Richter quote, he's saying that comparisons between upper case strings are optimised. NOT that converting to upper is any more efficient than converting to lower.Pontiac
Have to go with @Leptosome here, just tell the comparison function what you want it to do, it's optimized already anyway -- not leveraging the frameworks built-in features to roll your own is just ridiculous.Acrimony
C
407

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>()); } }
Chemical answered 12/8, 2008 at 16:32 Comment(23)
Now that is a cool technique: in-lining the assignment when null!Imperil
Don't you find it hard to read?Jerilynjeritah
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.Gaussmeter
Lazy instantiation is somewhat malpractice because it's a poor mans choice to avoid thinking about class invariants. It also has concurrency issues.Suboceanic
I usually use it as .. return _foo = _foo ?? new List<Foo>(); ... IMO a little easier to read.Yearwood
Sorry, misread what you wrote @chakrit... Its not bad, but it doooes take about 2 ms more to execute yours...Gaussmeter
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.Reedreedbird
@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.Gaussmeter
@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.Oarlock
@Oarlock 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.Gaussmeter
+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 outMetacarpal
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.Swallowtail
@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.Gaussmeter
@Chemical 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.Mt
@Charlie Flowers, putting initializer code in a getter is a Ruby standard practice (Ruby idiom)?!Zygophyte
@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.Carib
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.Ringer
I do this all the time... but interesting side note... reflector can't figure it out! Try reflecting an assembly in which you did this, and it'll tell you that the code was obfuscated ;) (at least it did before I quit my .Net job to start my own company...)Trilobate
What if you have a race condition to this property? It may cause double initialization the field, each thread getting a different copy. That's not good. This is not a failure of the operatior, may be using lock? read-write lock? interlocked? a nested class? Lazy<T>?Poltroonery
@Theraot: Yes, this is not thread safe. Of course, if you expect to be accessing this from multiple threads, you might want to try something different, such as what you stated.Gaussmeter
@Poltroonery Wouldn't Lazy<T> be a better idea anyway?Trapeze
@riezebosch, I did mention it, didn't I? Left last for dramatic effect or just because I was unable to use .NET 4 in production at the time... (it has to do with mono and linux). Apart from Lazy<T> the other has their place too although this is not a good place to discuss that.Poltroonery
Not thread safe, hard to read, confusing when used with a setter. This is no good.Goddart
C
314

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).

Crumbly answered 12/8, 2008 at 16:32 Comment(26)
Interesting tip! Personally, I use helper extension methods from my project (codeplex.com/thehelpertrinity) to simply do this: Click.Raise(this, "foo");Empyrean
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.Favien
you can still set your event handler to null, so you can still get a null reference, and you still have a race condition.Fluent
Also, performance-wise, it has to call an empty method every time.Thunderclap
I really don't like that this approach gets so much attention. The performance hit you take is actually somewhat substantial.Nab
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.Thralldom
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!Velamen
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.Velamen
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.Wharfinger
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.Luong
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!Squad
Can someone with rep modify the post, to include information about the performance cost? Not everyone reads the comments.Arbil
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.Pokelogan
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.Rattlehead
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.Blunder
Also see this post for more details and alternatives: blogs.msdn.com/ericlippert/archive/2009/04/29/…Blunder
I prefer using extension methods for event firing, I use a slightly modified (for increased thread safety) version of the technique at elegantcode.com/2007/12/05/firing-events-with-extension-methodsRincon
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.Maidstone
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.Weinberger
@Pavel, I 100% agree with you, I can never imagine a situation that you would want an event to be "null" instead of an empty list. The funny thing is that in VB.NET it is actually like you say already. So it's really weird that it's not like that in C#.Footstep
There should have been such a function built into the framework.Bettyannbettye
I use an extension method Raise<T>(this EventHandler<T> handler, object sender, EventArgs<T> args) that does a check against null in its body -- that way I can just write MyEvent.Raise(this, args) without needing to care about the check.Adhamh
I often do this with an empty lambda: public event MyClickHandler Click = (a, b) => { };Gwenngwenneth
In the end the only high performance way is the original. Empty delegates are a hit, single empty subscriber is a hit, an extension method is a hit. Doing the null check within the code itself is the fastest method.Gillum
How is extension method a hit? 1 function call :-)Galvanism
I wish the null check could be generated by compiler.Irremeable
L
304

Everything else, 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();
Luong answered 12/8, 2008 at 16:32 Comment(26)
I've actually needed to do the first one on a project using Anthem, because it clobbers the WebControls namespace.Virtue
in #3 you can do Enumerable.Range(1,5)Solidify
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 }Luong
i think you've been able to initialize arrays with int[] nums = {1,2,3}; since 1.0 :) doesn't even need the "new" keywordTrichina
You can, but now you can also do this: var lst = new List<int> { 4, 8, 15, 16, 23, 42 }Luong
also lambda without parameters ()=> DoSomething();Bourg
I weep for auto properties not getting default valuesBrick
@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 abilityBoleyn
I've used both { get; internal set; } and { get; protected set; }, so this pattern is consistent.Luong
@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)Boleyn
The namespaces aliases remind me of the yucky c++ :-SVolkman
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); }Sihon
@Sihon - 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 forLuong
or web.Control aWebControl = new web.Control();Cadmarr
@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.Luong
this last namespace trick is really useful to override an existing one. See on my blog for example! alsagile.com/archive/2010/03/09/…Audio
You can have as many parameters to your lambdas as you'd like: (n1,n2,n3) => n1+n2+n3;Mord
@L. Kolmodin - yeah you can - my 2nd point is that when you just have 1 you can skip the parameters: (x) => becomes x => Luong
//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!Epi
@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-typingLuong
@"1) implicit generics (why only on methods and not on classes?)" -- this can be alleviated for constructor calls through wrapping the constructor like this: class C<A,B> { public C(A a, B b) { ... } } static class C { public C<A,B> New<A,B>(A a, B b) { return new C<A,B>(A a, B b); } class Main { public void main() { var c = C.New(someA,someB); // type params inferred }Strikebreaker
@Strikebreaker - I know, which makes it even less logical that you can't, see my question on it: stackoverflow.com/questions/45604Luong
@Luong and @lukas: it is duck-typing but you also have to implement System.Collections.IEnumerable. You can get really crazy with void Add overload: new MyClass { 1, "a", { 1, "a" } };. See this questionUnduly
@Unduly - good point, I guess that's because Add on its own could mean something else, so List<int> x = new List<int>{1, 2, 3, 4, 5 } should collection initialise and makes sense. However int has an Add method but int x = new int{1, 2, 3, 4, 5} makes no sense.Luong
Number 3: But this doesn't work : byte[] get_bytes(){return {};}!!!Louise
@LimitedAtonement sorry, it's a little confusing. The { syntax for add doesn't work when the collection is empty.Luong
A
285

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.

Alfonse answered 12/8, 2008 at 16:32 Comment(12)
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.Taxidermy
Yes, what's specified in this answer is only one of the differences between (ToType) and "as ToType".Unseat
The "as" keyword also offers better performance :-)Furl
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.Taxidermy
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.Lidalidah
Right along the same lines of the "as" keyword... the "is" keyword is just as useful.Straus
It should be noted that this only works on nullable classes. You have to use int? instead of int.Cleo
You can abuse it and have a NullReferenceException down your road later when you could have had a InvalidCastException earlier.Volkman
@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.Baker
@SnOrfus @Lidalidah 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.Tack
Here's some inside info on the matter: "The specification is clear on this point; as (in the non-dynamic case) is defined as a syntactic sugar for is. However, in practice the CLR provides us instruction isinst, which ironically acts like as"blogs.msdn.com/b/ericlippert/archive/2010/09/16/…Tumbrel
as and casts are implemented differently, so the choice is more than just a matter of taste. See stackoverflow.com/questions/6099617/…Backstretch
W
261

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()}
Wolverine answered 12/8, 2008 at 16:32 Comment(13)
This makes classes much quicker to write.Climax
Should it be noted that Automatic Properties are a C# 3.0 only feature?Kordofanian
The great thing is typing "Prop" 2x[tab] and you get the property for free :)Espino
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!Esbensen
Something many people don't realise is that get and set can have different accessibility, eg: public string Name { get; private set;}Smallman
Only problem with Automatic Properties is that it doesn't seem to be possible to provide a default initialization value.Ousel
NValidate doesn't like automatic properties...Aramaic
@Glytzhkof : yes it is, see [DefaultValue()] in https://mcmap.net/q/16352/-hidden-features-of-c-closed/9035#9035 ( msdn.microsoft.com/en-us/library/… )Leasia
...I always provide default values for Automatic Properties in my constructor. Will look into the DefaultValue attribute!Nowt
@Leasia : 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).Rizika
@Roger Lipscombe: thanks plenty for the correction. You are absolutely right - msdn.microsoft.com/en-us/library/…Leasia
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 obviouslyJestude
@Guillaume86: I actually did that for a certain class hierarchy where I was using an Option type (in C#). Obviously Option types should initialize to None (the whole point is that they should be non-nullable). My base class constructor inspected the object for Option type properties and initialized them all to None.Gloxinia
B
254

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.

Bluetongue answered 12/8, 2008 at 16:32 Comment(1)
Plus: type? as a shortcut for Nullable<type>. default(int) == 0, but default(int?) == null.Ajar
C
225

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

Communalize answered 12/8, 2008 at 16:32 Comment(1)
Using DebuggerDisplay Attribute (MSDN): msdn.microsoft.com/en-us/library/x810d419.aspxPontius
W
220

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
Wolverine answered 12/8, 2008 at 16:32 Comment(5)
wouldn't the string include all the spaces that you used for indentation?Tichonn
Yes it's called verbatim string.Briolette
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.Iciness
Very useful for escaping regular expressions and long SQL queries.Stoa
It also maps {{ to { and }} to } making it useful for string.Format().Founder
C
219

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.

Cookgeneral answered 12/8, 2008 at 16:32 Comment(4)
I have a reflection utility class that does the same with FieldInfo, PropertyInfo, etc...Irmine
Yes, this is great. I was able to use methods like this to write code like EditValue(someEmployee, e => e.FirstName); in my business logic and have it automatically generate all the plumbing logic for a ViewModel and View to edit that property (so, a label with the text "First Name" and a TextBox with a binding that calls the setter of the FirstName property when the user edits the name, and updates the View using the getter). This seems to be the basis for most of the new internal DSLs in C#.Gloxinia
I think these aren't so much lesser-known as lesser-understood.Gwenngwenneth
Why do you need to register a method? I have never used this before - where and when would this be used?Booty
B
208

"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: kokos, thanks 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 MSDN documentation page for that operator: ?? Operator (C# Reference)

Bryon answered 12/8, 2008 at 16:32 Comment(4)
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.Espino
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.Bryon
The name for the ?? operator is the "Null Coalescing" operatorForlini
yield is my favorite, the coalesce operator is up there though. I don't see anything about CAS, or assembly signing, strong names, the GAC... I suppose only CAS is C#... but so many developers have no clue about security.Acrimony
C
197

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

one more thing about Nullable type:

DateTime? tmp = new DateTime();
tmp = null;
return tmp.ToString();

it is return String.Empty. Check this link for more details

Camber answered 12/8, 2008 at 16:32 Comment(10)
whats the difference between this and just using the wrapper objects fro primatives?Prudi
some types are nullable normally, like DateTime. So, DateTime? is a godsendBally
DateTime cannot be set to null.Container
This is effectively C# syntactic sugar for Nullable<T>, so is merely possibly by use of generics.Shabbygenteel
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.Velamen
@P Daddy - yes, int is syntactic sugar for System.Int32. They are completely interchangeable, just like int? === Int32? === Nullable<Int32> === Nullable<int>Besot
@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)Velamen
... 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.Velamen
had no clue data types that were once hated by null can now work as a null. :D Great to know!Taipan
@Brad Barker: tmp.ToString() returns empty string because it returns "the text representation of the value of the current System.Nullable<T> object if the System.Nullable<T>.HasValue property is true, or an empty string ("") if the System.Nullable<T>.HasValue property is false."Enchanting
W
192

Here are some interesting hidden C# features, in the form of undocumented C# keywords:

__makeref

__reftype

__refvalue

__arglist

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.

Weft answered 12/8, 2008 at 16:32 Comment(3)
More details on the undocumented keywords by Peter Bromberg. I still don't get if there are reasons to ever use them.Unduly
@Unduly with the advent of generics, there aren't many (any?) good reasons to used __refType, __makeref, and __refvalue. These were used primarily to avoid boxing prior to generics in .NET 2.Sybille
Matt, with the introduction of generics in .NET 2, there's little reason to use these keywords, as their purpose was to avoid boxing when dealing with value types. See the link by HuBeZa, and also see codeproject.com/Articles/38695/UnCommon-C-keywords-A-Look#udSybille
H
184

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;

Hyperpyrexia answered 12/8, 2008 at 16:32 Comment(7)
@George, works wonders when you are communicating with legacy apps over sockets using c union declarations.Headed
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.Suboceanic
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.Proctor
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.Bryant
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).Crabby
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.Lesleelesley
@desco, I imagine this could only possibly apply to value types, and not reference types (arrays are reference types).Gustav
D
175

Using @ for variable names that are keywords.

var @object = new object();
var @string = "";
var @if = IpsoFacto(); 
Derbyshire answered 12/8, 2008 at 16:32 Comment(20)
Why would you want to use a keyword as a variable name? Seems to me that this would make code less readable and obfuscated.Goldbrick
Well, the reason that it's there is that CLI requires it for interoperability with other languages that might use C# keywords as member namesDerbyshire
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! :)Sihonn
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 keywordEspino
Great for extension methods. public static void DoSomething(this Bar @this, string foo) { ... }Shipwreck
This is not a feature, @ is just allowed symbol for naming like _ and many others (name '@this' will not be equal to name 'this')Stemma
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.Myrmeco
@zihotki: Wrong. var a = 5; Console.WriteLine(@a); Prints 5Urien
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.Piecework
It can also be used to signify that some variable is used as a SqlParameter, var @pMyParameter = 1; new SqlParameter("@pMyParameter",@pMyParameter);Suboceanic
Personally, i think Class @class is better than Class clazzPolypary
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.Fritillary
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.Streamer
This is also useful in applications where "Events" are a type of business objects (for example planning tools). Being able to name the business object @Event instead of inventing a synonym is really nice. Especially if event is the word used in the "real world": instead of trying to remember what I mean by "happening", I can just call things by their names, regardless of what words the language has reserved.Soughtafter
Event is a valid identifier without an @. But you do need an @ for @event.Derbyshire
When dealing with xsd auto-generated xml-based classes, escaping the element name is sometimes a must. for instance: <event /> = @eventRespect
Could also be handy if a new brew of C# come up with a new keyword that happens to be widely used in your existing code :)Malocclusion
C# isn't likely to ever have any new reserved keywords and any new context-dependent keywords are likely to be overridden by existing code, like with var which will only be used to automatically infer the type if a type named var doesn't exist.Derbyshire
Don't forget :: can be used for using aliases where a class name and an alias conflict. I.e. using xyz=System.Windows.Forms; xyz::Form (as opposed to xyz.Form).Acrimony
The @ sigil is to mitigate conflicts between languages. The :: operator is used to differentiate identical names for classes from different assemblies.Derbyshire
C
167

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

Environment.FailFast()
Corr answered 12/8, 2008 at 16:32 Comment(4)
Note that this method also creates a memory dump and writes a message to the Windows error log.Hanselka
It should be noted no finalizers or app domain unload events will be called when this method is used...Breaker
+1 for the hint, but this is not C#, it is BCL of .NET.Unmeet
System.Diagnostics.Process.GetCurrentProcess().Kill() is fasterReproval
S
153

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 t)
{
   return (T) obj;
}
Socialize answered 12/8, 2008 at 16:32 Comment(12)
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.Container
@Jason I did say it's probably not useful but it is surprising (and I thought hidden).Socialize
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.Bullroarer
I could see this being useful if you have some sort of convention running throughout your application when you return tuplesSmoky
@George: such a convention would be called a... struct?Address
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. ;-)Weinberger
this trick is named 'cast by sample' and it will not work if method that returns anonymous type is located in another assembly.Lesleelesley
@desco: "hidden features" not necessarily useful.Socialize
@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...)Vinnievinnitsa
@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.Weinberger
@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)...Vinnievinnitsa
weird. My co-worker and I actually wrote this exact function at a former job.... we had a (semi-)legitimate use case for it, but ended up just making a new type and removing the function before someone used it for evil ;)Trilobate
C
146

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.

Cancroid answered 12/8, 2008 at 16:32 Comment(5)
Also, a @ constant accepts newlines inside. Perfect when assigning a multiline script to a string.Backstretch
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]Archivist
I tend to build paths with Path.Combine. I definitely use the @ for regex!Cleo
@new is also a variable instead of a keyword: @this, @int, @return, @interface...so on :)Manhour
This one doesn't come anywhere near: But what are the most hidden features or tricks of C# that even C# fans, addicts, experts barely know?Durango
C
141

Mixins. Basically, if you want to add a feature to several classes, but cannot use one base class for all of them, 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!

Chancroid answered 12/8, 2008 at 16:32 Comment(6)
Yeah I think this is the real power of extension methods. They basically allow for implementations of interface methods.Meaghan
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.Adcock
That's basically how all of the LINQ methods are implemented, for the record.Ringer
Here's a link that talks about what I referred to above, using extension methods with collection interfaces in NHibernate or Castle ActiveRecord: devlicio.us/blogs/billy_mccafferty/archive/2008/09/03/…Adcock
If only they allowed extension properties!!!! I hated having to write an extension method that begged to be a read-only property....Trilobate
It's not strictly a mixin unless you can decorate a class with an interface after the fact - but still cool :)After
B
130

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

True, False, FileNotFound?

Bryon answered 12/8, 2008 at 16:32 Comment(23)
if expect a user to answer a yes no question then null would be appropriate if the question has not been answeredSferics
I used a nullable bool when populating a ddl of employees. Active only = true. InActive only - false. Active and Inactive = nullArmorer
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?"Adversative
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.Bryon
Nullable types are handy for interaction with a database where table columns are often nullable.Leonidaleonidas
Yes, No, Maybee?Massproduce
Store values of a ThreeState CheckBoxGoff
As in SQL: Yes/no/unknown.Dott
Using enum is a better approach instead of using nullable booleanSoprano
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.Bryon
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?Mattins
Utterly diffident, one fails to answer Yes and No question with a Maybe.Hurl
@Dott you mean 1 / 0 / NULL, a Nullable bool field.Goff
Nullable<bool> doesn't make sence to me either, as a boolean is "Yes/No", there is no maybe...Bandit
@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.Bryon
@Mattins 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 representationKoblas
@Dan Blair: I don't know, could you repeat the question?Adhamh
There are lots of uses for trinary logic. Yes|No|Undefined, Yes|No|No-Response, Yes|No|N/A, etc. Really anything that has 3 possible states.Gwenngwenneth
True, False, not yet setKeelung
SQL is the main reason. Also nice as a trycast: var a = expr as bool?; if (a != null){ // do something with a.value} else { // expr wasn't a bool }Trilobate
@Dan Blair: In portuguese the Word for Yes is Sim, the word for No is Não. In recent political speeches, it was invented a new word: "Nim". A mixture of Yes and No!Jimmie
The reason to use a Nullable< bool > is to accurately answer questions like, "Have you stopped beating your wife?". Yes, No or N/ACandiot
For some of you commenters, I'd suggest not thinking of null as just another value. Null should be restricted to situations where it means "don't know", "don't care", or "not applicable". Ideally, we wouldn't even use it for the "no value" case, but that is hard to avoid. Once you have this in mind, it is easy to think of scenarios that you would want a bool?. All that being said, most often using a reasonable default value is better than relying on null references.Idolize
O
116

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.

Openandshut answered 12/8, 2008 at 16:32 Comment(8)
Select force you to always return something from the supplied expression wherears map is less picky about that.Yearwood
Select also acts like the "return" function in monads. See https://mcmap.net/q/16352/-hidden-features-of-c-closed#405088Derbyshire
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.Openandshut
@Brad, that (where) is a filter operation. Try doing a map operation without select...Ballon
LINQ is the big thing that happened to C# in my opinion: stackoverflow.com/questions/2398818/…Historiography
Aggregate's smallest signature is the "reduce" function, Aggregate's middle signature is the much more powerful "fold" function!Bucella
Select is very powerful, for examples such as: string[] fileNames = a_directoryInfo.GetFiles().Select(fileInfo => fileInfo.Name).ToArray();Holothurian
This drives me nuts. I always go through the same process: "I need to do a map, let's see that would be... wait... wait... select!"Founder
U
115
Environment.NewLine

for system independent newlines.

Uracil answered 12/8, 2008 at 16:32 Comment(3)
The annoying thing about this one, is that it isn't included into the compact framework.Parolee
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.Nephogram
This is part of the BCL of .NET, not a feature of C# per se.Unmeet
R
111

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"
Radicle answered 12/8, 2008 at 16:32 Comment(7)
@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 en.wikipedia.org/wiki/Bracket.Amarelle
You are correct. But when I commented, it didn't say "curly" brackets.Adcock
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 :)Telegram
Muahahahaa... Programming already 3 years in .Net and I didn't know this one. :DPaid
"curly" brackets acting similar to escape sequences ??Bottommost
@Kyralessa, @icktoofay: we should try to stop being programmers in conversation :) See jwz vs. BrandenGolub
See also stanford.edu/~pgbovine/geek-behaviors.htmAdcock
M
104
  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
Moult answered 12/8, 2008 at 16:32 Comment(8)
"using - great keyword that can be used for more than just calling Dispose" I am intrigued, can you elaborate on that?Romola
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: msdn.microsoft.com/en-us/library/sf0df423.aspxOmphale
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.Zipnick
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");Tonguelashing
Just in case you'd like a name to #1 (as you did with the ternary operator), ?? is called the null coalescing operator.Meany
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.Tawana
@LucasAardvark: As J Steen mentioned it's called the null coalescing operator. Search for that!Overcloud
To search for ?? operator at google try: google.com/search?q=c%23+%3F%3F+operatorCroatia
A
103

@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.

Allegory answered 12/8, 2008 at 16:32 Comment(12)
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.Empyrean
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.Velamen
FxCop will warn you if you place a 'as' inside an 'if is'.Shipwreck
not knowing C#, but borrowing from c++, does this work? if(MyClass c = obj as MyClass) { ... }Chemical
Will: That won't work in c#, because null and false cannot be compared to each other.Toein
No, but you could write "if ((c = obj as MyClass) != null)".Rubio
@P Daddy: as Kent said, as casting is faster anyway. I've also benchmarked this and found this to be the case.Catoptrics
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.Gareri
@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.Ringer
It seems to me to just try to use it as what you expect it to be, wrap it in a try/catch and then handle the error appropriately if it occurs. As in an override Equals(object arg){ try{ return ((MyClass)arg).Id == this.Id; } catch{ return base.Equals(arg); }}Holothurian
In IL, a cast goes to a CASTCLASS but an as/is go to an ISINST instruction.Trilobate
I ran a test for this, casting IEnumerable<int> to List<int>, and casting object (new object()) to IEnumerable<int>, to make sure there are no mistakes: direct cast: 5.43ns, is->as cast: 6.75ns, as cast: 5.69ns. Then testing invalid casts: direct cast: 3125000ns, as cast: 5.41ns. Conclusion: stop worrying about the 1% factor, and just make sure you use is/as when the cast might be invalid, cause exceptions (also if handled) are VERY slow compared to casting, we're talking about a factor 578000 slower. Remember that last part, the rest doesn't matter (.Net FW 4.0, release build)Polemic
A
98

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;
Algarroba answered 12/8, 2008 at 16:32 Comment(17)
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.)Sadfaced
"This" is called the "ternary operator". en.wikipedia.org/wiki/Ternary_operationRadicle
The ternary operator has been banned in my group. Some people just don't like it, though I've never understood why.Indiaindiaman
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 statementBriannabrianne
can be condensed to x++; Ok it's pointless ^^Herculaneum
@Guillaume: To account for all values of x: x = 2 + System.Math.Min(1,System.Math.Abs(x-1));Nomo
It's actually called the "conditional operator" - it is a ternary operator because it takes three arguments. en.wikipedia.org/wiki/Conditional_operatorCherida
@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.Pardon
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: stackoverflow.com/questions/1816859/…Pardon
@Roman those aren't statements, just expressions.Flake
@Flake Thanks. There are multiple definitions of statements vs expressions which got me confused. Some useful explanations: msdn.microsoft.com/en-us/library/ms173143.aspx (switch to C# tab), and https://mcmap.net/q/16520/-expression-versus-statement/…Pardon
x = x == 1 ? ((Func<int>)(() => { Console.Write("This is a statement"); return 2; })).Invoke() : 3;Lanalanae
string s = x==1? "one" : x==2? "two" : x==3? "three" : "zero";Mika
@Martin cool, but I have to say its difficult to readHaplite
@Haplite I agree, I have heard its the death penalty in most countries for anyone who does it tooLanalanae
I use the ternary operator when needed, and hate it every time. It is so ugly and destroys readability. An expanded if/then statement is so much nicer to read.Careerism
@François: No, this isn't the same as x++... Don't assume x is either 1, 2 or 3... it can be anything.Champaigne
T
94

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 ) ){ ... }
Territoriality answered 12/8, 2008 at 16:32 Comment(2)
This could be changed quite easily to be null-safe as well: var isEqual = String.Equals(a, b, StringComparison.OrdinalIgnoreCase);Bronchia
But... this isn't a C# feature, it is a feature of the .Net framework, more specifically a feature of the class "String"Champaigne
F
79

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

string hello = "world";
var o = new { hello };
Console.WriteLine(o.hello);
Fourflush answered 12/8, 2008 at 16:32 Comment(1)
this is because anonymous types are not anonymous after compilation.Herra
M
75

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 matching items against the subject of the search can be really quite useful + succinct.

Melton answered 12/8, 2008 at 16:32 Comment(14)
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.Sadfaced
you probably wanted fruit == banana?Ahithophel
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.Sparky
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. :)Paid
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 :)Impulsion
@Impulsion 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 :)Herriott
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; }Velamen
except that in c# we don't have fallthroughDevest
@P Daddy - true, but does suffer from a lot of additional syntax. @Devest - you can have fallthrough if the cases have no code (until the fallen-through case of course).Herriott
@Fowl, yes we have, only for empty case statements, like this.Albemarle
public static bool In<T>(T value, params T[] items){ return items.Contains(value); } if (fruit.In("apple", "orange", "banana", "pear")) { ... }Trilobate
This code creates new array each call. Actually it is incorrect when you create an new object when you really need to verify an argument. And please, dont tell me about readability. Semantically wrong syntax can not be readable. And short code/nicely put braces is not readability.Ogilvy
@Belorus I think you're treating it as if readability were a science, which it is not. Yes, it is a performance hit, but very often it will make no difference to the overall performance of the program. How is it 'wrong' if it does the job, even if it needlessly allocates memory? Readability is a very subjective thing - what seems simple to one person can seem appalling to another.Herriott
For me this is a classic example of bad programming. Terrible performance, bad readability. (Yes, second statement is subjective.)Pushover
H
75

Honestly the experts by the very definition should know this stuff. But to answer your question: Built-In Types Table (C# Reference)

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
Hacking answered 12/8, 2008 at 16:32 Comment(8)
Every time I'm dealing with decimals, I have to look up the m. Is it only me or is m not really intuitive? :)Myrmeco
The M syntax comes from the old VB type called Money. M == Money == Decimal.Hacking
Nope, anything less than an Int32 is automatically inferred by the compiler based on the type to the left of itHacking
@Nick: nice - I like learning the historicals behind the code.Manhour
@Myrmeco : and I guess the D was already taken :)Laing
@Nick, Thanks for the history of m for decimal, this will help me remember this one. The one I never use is D for double; I always prefer writing 30.0 and curiously I regularly use the L for long.Heredity
It's interesting to note that though the lower case variants are valid, using l for long is discouraged because it can be easily mistaken for 1.Heredity
I remember M=Decimal/D=Double the same way I remember port/starboard: starboard means "Right" and has more "R"s. Decimal has an M but Double does not.Cutcheon
S
73

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.

Scheer answered 12/8, 2008 at 16:32 Comment(3)
I use this regularly for writing unit tests against internal members of another assembly. That way unit tests can be excluded from deployments.Fussbudget
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.Colpin
InternalsVisibleTo is very useful for exposing a protected internal parameterless .ctor to a NHibernate layer whilst hiding it from a user application which can then only use the public .ctor that enforces supplying required data.Kelvin
R
72

Here is a new method of the string class in C# 4.0:

String.IsNullOrWhiteSpace(String value)

It's about time.

Reese answered 12/8, 2008 at 16:32 Comment(6)
What's the problem with making your own util method that returns this: (myString ?? "").Trim() == ""Isotone
@Charlie Isn't a carriage return treated as whitespace too?Hesperidium
I definitely prefer Haack's approach 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".Gawen
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.Gawen
I really don't like "". You should use string.Empty (myString ?? string.Empty).Trim() == string.Empty as it conveys a bit more.Candiot
On the record, I absolutely HATE string.Empty. What could it ever be besides ""? The performance benefit of a static field vs an interned string will be negligible: stackoverflow.com/questions/263191/…Trilobate
W
70

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.

Wellfixed answered 12/8, 2008 at 16:32 Comment(3)
not for Debug.WriteLine though cuz it uses a #if DEBUG statement internally.Ineffectual
@AndyC: It's not about that here (ForEach is simply defined as a custom extension method somewhere. There are religious debates over that elsewhere :))Golub
IMHO, the following looks better ----------------- foreach(string s in myString) Console.WriteLine(s);Ogilvy
S
68

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!

Slope answered 12/8, 2008 at 16:32 Comment(0)
A
68
  • 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...

Appendicle answered 12/8, 2008 at 16:32 Comment(8)
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.Vagal
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.Suboceanic
Completely bonkers? Well, I found .ForEach to be faster than all other options. See jerrytech.blogspot.com/2010/02/… if you doubt me. Always doubt me ;) The code's there - run it yourself and see.Homebrew
On the other hand, Patrick Smacchia says the opposite: ForEach is slower than For. codebetter.com/blogs/patricksmacchia/archive/2008/11/19/…Zygophyte
There's now also String.IsNullOrWhitespaceKallick
One caveat - note that TransactionScope's default Isolation is ReadSerializable (whereas the usual default is just Read Committed). This can cause a lot of deadlocks unless you override your TS. See blogs.msdn.com/b/dbrowne/archive/2010/05/21/…Touraco
@JerryNixon Your test is completely bonkers. And the timing results you posted don't even fit the code you provided.Traditor
@JerryNixon Your graphs look about right for that code but I don't think you are testing what you want to be testing. Either go with: using (var junk = m_Source.GetEnumerator()) for (; junk.MoveNext();) _Target1.Add(junk.Current + 1); or for (int i = 0; i < m_Source.Count; i++) // Count propertyJudoka
U
66

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.

Upu answered 12/8, 2008 at 16:32 Comment(3)
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.Oleaster
TryGetValue throws if the key is null -- so much for avoiding axceptions. I use a TryGetValue2() extension method to get around this problem.Whiskey
Looking up a null in a dictionary seems more likely a code error than looking up a non-existent value. I personally am glad that it throws the exception ;)Trilobate
G
65

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)
Georama answered 12/8, 2008 at 16:32 Comment(3)
This is similar to reg expressions, very useful, but I can't remember them either. I handle stuff like above with padleft and padright.Leonidaleonidas
Cool, I never knew it was possible... Is it documented anywhere ?Yell
@Thomas: It's documented in MSDN in the "The ";" Section Separator" section towards the end of the Custom Numeric Formatting topic at: msdn.microsoft.com/en-us/library/0c899ak8.aspxAntipersonnel
D
64

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;
Derbyshire answered 12/8, 2008 at 16:32 Comment(0)
C
61

Don't forget about goto.

Carpophore answered 12/8, 2008 at 16:32 Comment(10)
No, lets forget it. ;)Admonitory
No, lets abuse it till kingdom come. ;)Yearwood
Yeah, don't forget it--make sure to nuke it when you have a chance!Sennight
from the old days of AppleSoft basic: gosub/return ;)Manhour
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.Belonging
#region hidden / goto haiku_secretly / #endregion // hiddenGolub
+1. How do you break out from multiple levels of loops without messing with bool variables or moving the entire thing to a function or lambda? There should be a reason why does it exist in the language...Maccabees
Two or three levels deep in nested loops and this is an absolute godsend. Can't think of any other possible legitimate reason to use it though.Homs
@Homs - how about fall-through in switch statements? Goto is perfect for this.Ulibarri
@Maccabees I've seen parametrized "break" and "continue" in other languages, like "break 3" to break up to the third for/while/etc. It's also prone to errors, because if you inadvertently add a new inner loop in the code, the "break 3" might break the wrong loop. Goto is a cleaner way to do that, IMHO. A better way to solve this would be to have named loops like "for(....) as myloop { .... }" and then "break myloop", but I didn't see that in any language.Leveller
L
56

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.

Lael answered 12/8, 2008 at 16:32 Comment(4)
Nice tip. You should move this question to the Hidden .NET Base Class Library question stackoverflow.com/questions/122784/…Godesberg
Awesome. I've got a multicore webserver; its extra core and memory have been going to waste!Laurinelaurita
This optimization works well for me on IronScheme too :)Trill
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.Prut
P
55

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.

Puli answered 12/8, 2008 at 16:32 Comment(0)
S
55

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
Sludgy answered 12/8, 2008 at 16:32 Comment(3)
Instead of 32767 and a comment, how about short.MaxValue?Jemine
Just reminding everyone what the exact MaxValue is!Sludgy
perhaps we should list '32767' as the max value for a short as a hidden language feature of itself? (I think it is hidden programmer ethics to hardcode the number)Golub
M
55

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;
}
Mcgary answered 12/8, 2008 at 16:32 Comment(8)
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!Espino
That's great. I always used that calling the base, but never knew you could use it in the same class!Sadfaced
can you guys link to somewhere showing a usage example of this for cross class and base class?Susurrate
Yea, I use this all of the time. It really is handy, and very intuitive I think.Baker
I think you meant: public Example(int value1) : this(value1, "Default Value") { }Hairball
Well spotted rball! 1 year, 21 upvotes and nobody spotted my deliberate mistake ;) Fixed.Trespass
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") {...}Natividad
Also you can use this similar technique when you inherit base classes. Similar usage: ": base(value1, "Default Value")"Cap
G
48

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.

Geoffreygeoffry answered 12/8, 2008 at 16:32 Comment(7)
Interesting Visual Studio feature, but the question is about C#.Option
+1 There is a section "Visual Studio Features" in this question, where your answer fits perfectly.Pardon
Also it worked just by hit on Alt. Not need to press and hold Alt.Grantland
It's not just Visual Studio feature. you can do this in Microsoft Word Notepad2 etc. etc.Yearwood
Very limited compared to Vim's block selection, though.Contradict
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.Artificer
This is called vertical selection in many text editorsMcelrath
B
48

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.
Butyrin answered 12/8, 2008 at 16:32 Comment(1)
Unfortunately, stackalloc requires unsafe context.Hanselka
U
46

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

Underbred answered 12/8, 2008 at 16:32 Comment(1)
Hey, this is not exactly true. The volatile keyword instructs the compiler to generate an acquire-fence on every read from a field, and a release-fence on every write to the field. An acquire-fence prevents other reads/writes from being moved before the fence; a release-fence prevents other reads/writes from being moved after the fence. Read more here: bit.ly/hycbVIPhoebephoebus
F
46

@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);
Friday answered 12/8, 2008 at 16:32 Comment(2)
Why not do this Console.WriteLine( "".PadRight( 22, '-' ) );Curch
To achieve shorter and cleaner code, for example.Friday
K
45

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”);
Kruter answered 12/8, 2008 at 16:32 Comment(1)
I used params for forbidding the empty constructor in classes: stackoverflow.com/questions/6273404/…Jimmie
C
45

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 attribute:

 [MethodImpl(MethodImplOptions.Synchronized)]
 public void MySynchronizedMethod(){}
Cence answered 12/8, 2008 at 16:32 Comment(5)
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 :)Fussbudget
[MethodImpl(MethodImplOptions.Synchronized)] = lock(this) = badLippizaner
I'm not familiar with "Sychronized" method, can you tell me what they do?Gun
"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.Novelize
@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).Baker
C
44

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!");
}
Crumbly answered 12/8, 2008 at 16:32 Comment(2)
That's awesome! I didn't knwo that... However, same thing with collection initializers... all you need is an Add(x) method. public class MyList{ public void Add(string s){} }. You can then do var l = new MyList{"a", "b", "c"};...Trilobate
@JohnGibb: Having only an Add method is not sufficient. From section 7.6.10.3 of the C# spec: "The collection object to which a collection initializer is applied must be of a type that implements System.Collections.IEnumerable or a compile-time error occurs."Beghtol
M
42

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.

Milliemillieme answered 12/8, 2008 at 16:32 Comment(6)
No need to lock as all the static members are thread safe.Hoyden
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.Abdias
Okay, I completely removed the example and made everything more clear (I hope!).Abdias
@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.Velamen
&quotStatic constructors are also useful when creating wrapper classes for unmanaged code&quot, how about static destructor for unmanaged code? ;)Lodicule
FxCop doesn't like static constructors for performace reasons. K. Scott Allen's blog post throws some light on why: odetocode.com/blogs/scott/archive/2004/09/15/…Lowpressure
A
40

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; }
}
Ayo answered 12/8, 2008 at 16:32 Comment(4)
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.Bordelon
Hmm...neat trick but I think I'd take fields being visible in the debugger over having all that cruft in my classSmoky
Plus if this causes you to miss even one bug ever, it won't have been worth your time.Maronite
the usefulness of [DebuggerBrowsable(DebuggerBrowsableState.Never)] is for long database calls for objects like users that have Invoices, Addresses, Phones, CreditCards, etc that are each calls to fill up via database calls. If anyone of these fails to return in time, the whole state of the object returned is unreadable (in the debugger).Holothurian
P
39

C# + CLR:

  1. Thread.MemoryBarrier: Most people wouldn't have used it 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 exception:


#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();
Pneumoencephalogram answered 12/8, 2008 at 16:32 Comment(2)
Sweet, great tips, in the last one you had tag trouble... replace < with &lt; and we will be able to read it :)Hypnogenesis
Now most of this comes down to 'people don't know the mechanics behind threading' IMHO. That may be true, but doesn't come close to 'hidden language features'? Thumbs up for Debugger.Break()Golub
P
38

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 static T Append<T> (this System.Enum type, T value) where T : struct
    {
        return (T)(ValueType)(((int)(ValueType) type | (int)(ValueType) value));
    }

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

    public static bool Has<T> (this System.Enum type, T value) where T : struct
    {
        return (((int)(ValueType)type & (int)(ValueType)value) == (int)(ValueType)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...

Pheasant answered 12/8, 2008 at 16:32 Comment(4)
Nice; but I'd use Include instead of Append since Append implies an order which the values may not have.Antipersonnel
@Antipersonnel - that is a good point, I didn't really ever think about it from that perspective.Burden
@HBoss: I'm gonna use that. It will make it so much easier to implement Flags the way we should be.Manhour
Just figured something awesome out: All those "object" casts are causing a lot of boxing. If you add a generic constraint of where T : struct (so that T can't be a reference type), you can actually cast (int)(ValueType)value and avoid the boxing: I've edited the answer to show this.Trilobate
W
38

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);
Wirephoto answered 12/8, 2008 at 16:32 Comment(4)
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); }Susurrate
Lambdas are only available in C# 3.0 or greater. That means you must have .Net 3.5.Wirephoto
Thanks, luckily we've moved up to .net 3.5 thanks to my pushing for itSusurrate
+1 Link to closures question:https://mcmap.net/q/20114/-what-are-39-closures-39-in-net Article with explanation of compiler generated code blogs.msdn.com/b/abhinaba/archive/2005/10/18/482180.aspxLodicule
U
36

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.

Ultra answered 12/8, 2008 at 16:32 Comment(5)
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...Rayburn
Yes, I agree, and not completely sure why the MarshalByRefObject restriction exists...Ultra
Thanks for the detailed explanation! Although this is not really a feature of the C# language.Leggy
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 :)Zebrass
The type can also be an interface, not just MarshalByRefObjectSusurrant
W
36

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.

Weft answered 12/8, 2008 at 16:32 Comment(1)
See msdn.microsoft.com/en-us/library/ms173212(VS.80).aspxSybille
L
36

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;
Lederer answered 12/8, 2008 at 16:32 Comment(11)
But the values have to be discreet. So no floats or doubles etc. Just for completeness' sake ;)Espino
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?Podite
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.Lederer
One good reason would be to match it to the ID column of a read-only bootstrap table in a database.Radiotelephone
Also, note that you can assign MyEnum e = (MyEnum)123;, but when you access e you'll get an Exception.Leasia
You can also have [Flags] to tell that the enum is flag basedMatelda
@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.Kallick
@Podite - 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.Squad
@ANeves: you will never get an exception. ToString() will return the numeric value instead of the name. The only way you'd know is if you used Enum.IsDefined(MyEnum, e), in which case you'd return false.Trilobate
Like Luke Foust says, it's useful for flags. You can assign values 1, 2, 4, 8, 16...etc to the enums and thus add them up in a single int and use this to store a whole range of settings.Staphylorrhaphy
@John Gibb: I know you can get an exception out of it, out of empirical experience. But I'm not managing to reproduce it now and don't have the code where I tested for that. (Changed work places in the meanwhile.) I was using enums to map numeric values to human-readable code, and one of the tests tested for that. PS: ...oooow poke.Leasia
M
35

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.

Mcclain answered 12/8, 2008 at 16:32 Comment(5)
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"; }Ubiquitarian
+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.Parlin
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.Adcock
var xdoc = new XDocument( new XElement("root", new XElement("game", scores.Select(score => new XElement("score", score.ToString()))))); // apologies for lack of formatting - hosed by SOLangille
I just don't see how this is a language featureGolub
C
34

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;
Cori answered 12/8, 2008 at 16:32 Comment(10)
How would one employ a nullable type without using HasValue?Abbyabbye
Like this: int? x; if(x != null)Cori
No, people like to write: y = x ?? defaultvalue.Rubio
Just to be clear, (x != null) and (x.HasValue) result in identical IL.Bryant
I prefer x != null over x.HasValue.Leasia
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.Korney
Is there a difference between y = x.Value and y = (int)x?Gwenngwenneth
@Snarfblam it's interesting to point out that nullables are special cased by the .Net boxing code. For example, an Nullable<int> with a value will be boxed as an int, but a Nullable<int> without a value will actually be boxed as a null. If you wrote your own Nullable class, it would be boxed as a YourNullable<int> either way, and a comparison with null would NEVER be equal (since there actually IS a boxed value on the heap).Trilobate
Way too much typing ... y = x.HasValue ? x.Value : y or y = x ?? y...Champaigne
when trying ?? operator we need to typecast the type sometimes.like int? i;Madera
S
33

My favourite is the

global::

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

Example:

global::System.Collections.Generic.List<global::System.String> myList =
    new global::System.Collections.Generic.List<global::System.String>();
Steen answered 12/8, 2008 at 16:32 Comment(1)
It works just like an access specifier but with respect to namespaces i.e. global when used with the namespace alias qualifier :: refers to the global namespace, which is the default namespace for any C# program. Example usage here - msdn.microsoft.com/en-us/library/c3ay4x3d.aspxTroika
A
31

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();
Abbasid answered 12/8, 2008 at 16:32 Comment(5)
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.Disqualification
It is not a hidden feature. It's completely documented in the specification.Pt
@Lyubomyr and so are 90% of answers to this question.Cecilycecity
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 ...Ketch
Niklaos, checking the Il it's a compile time substitution and not something at runtime.Crawfish
M
31

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

Melodious answered 12/8, 2008 at 16:32 Comment(0)
I
31

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.

Impulsion answered 12/8, 2008 at 16:32 Comment(3)
You probably can't use your method if f2 depends on f1, right? I use the second method all the time where you "use" an NHibernate ISession and then use that to build an NHibernate ITransaction, which is also disposable.Gloxinia
Note that you can only specify multiple objects in the same using statement if they are of the same type.Langille
does this also contain the same disposal gotcha as property initializers in a using clause? where if one of the things in the using clause throws an exception, so something doesn't get disposed.Susurrate
J
31

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...

Johnniejohnny answered 12/8, 2008 at 16:32 Comment(5)
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 List And with a CI like Hudson that picks these up it's great!Hypnogenesis
In fact, you can add an extension method to everything with the generic extension methods...Crozier
RCIX, that's right but what would be the use of Generics? You could just define an extension method for object.Aricaarick
String.Join and Enumerable.Range remind me of Python's equivalents: ",".join(["a", "b", "c"]) and range(0, 15).Kallick
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)).Kallick
I
30

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;
}
Iolite answered 12/8, 2008 at 16:32 Comment(7)
-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.Aubreir
+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.Velamen
+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.Zel
+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.Manhour
@blesh I remember basic... on my old Acorn A3000. ;DHydrology
I agree that continue is wonderful, but I don't see how this is a hidden language feature? I've been using it since day one.Rockribbed
+1 Quads on JC - continue, break, and goto all have their place. For example in switch statements when you wish to fall-through...Non-structured flow is necessary for many algorithms. It's not evil, just complex.Ulibarri
F
28

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>>(() =>
{
    return new Dictionary<Int32, String>
    {
        { 0, "Foo" },
        { 1, "Bar" },
        { 2, "..." }
    };
})();

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.


Alternative syntax

// The one-liner
Func<Int32, Int32, String> Add = (a, b) => Convert.ToString(a + b);

// Multiple lines
Func<Int32, Int32, String> Add = (a, b) =>
{
    var i = a + b;

    return i.ToString();
};

// Without parameters
Func<String> Foo = () => "";

// Without parameters, multiple lines
Func<String> Foo = () =>
{
    return "";
};

Shorten a string and add horizontal ellipsis...

Func<String, String> Shorten = s => s.Length > 100 ? s.Substring(0, 100) + "&hellip;" : s;
Fractionate answered 12/8, 2008 at 16:32 Comment(0)
S
28

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)) {
    ... 
}
Stubbs answered 12/8, 2008 at 16:32 Comment(7)
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.Upanishad
Its's not specific to using, you can write: if(Something) using(new Pen()) using(new Brush())for(;;)DoSometing();Irmine
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();Reservoir
You can do this with every statement that can be nested.Margrettmarguerie
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)Selhorst
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;"Sophy
I use this to avoid unnecessary nesting but it's a bit smelly (as per Nissim's comment above)Langille
S
28

@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; }
Sparky answered 12/8, 2008 at 16:32 Comment(5)
This syntax was added to c# in 2.0. C++ allowed it on .Net 1.0 and 1.1Lumumba
Unfortunately this is the only applicable combination of distinct access rights, 'get; internal set;' would not workBoleyn
How do you figure? This compiled for me fine just now: public string Foo { get; internal set; }Sparky
protected set; is really useful for base classes.Paid
What would the point of a private property be anyway? what's wrong with 'this.Name='?Cence
D
28

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

  1. Snippets (particularly for properties, which was made even better for Visual Studio 2008)
  2. The ObsoleteAttribute
Denotative answered 12/8, 2008 at 16:32 Comment(7)
I like the switch snippet very much. Makes switching on an enum sooo much easier ;)Myrmeco
i regularly use a snippet for Properties calling OnPropertyChanged in the setter. very handy.Oxyhydrogen
Are snippets though a feature of Visual Studio? Rather than C#/compiler?Tawana
snippets are part of visual studio shortcut is ctrl - k+ctrl xSusurrate
better yet eg: write "for" or "switch" and then double-press 'tab' keyTelegraph
very handy indeed both with Ctrl-K + Ctrl-X and double tab on "for", "while", "foreach", "if", etc.Hurl
Yep, like obsolete for refactoring to highlight code I'm trying to make go away.Langille
P
27

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'

Pardon answered 12/8, 2008 at 16:32 Comment(5)
Actually, not bad. The first look at the sample is misleading. I'll remember this trick. :)Pt
Cant you simply write Console.WriteLine(iList.GetType().Name);?Starobin
@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.Pardon
Will that work in C# 2.0 or earlier also? not sure if generic type inference was there before 3.0.Lewis
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 csharpindepth.com/Articles/General/BluffersGuide3.aspxPardon
P
27

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 :-)

Popover answered 12/8, 2008 at 16:32 Comment(7)
I'm disappointed, so many years coding in .NET without using this syntax... Great !Michale
Unfortunately this feature is duped. Saw the same above.Paid
It isn't anymore in .NET 3.5.Repurchase
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.Popover
duplicate: https://mcmap.net/q/16352/-hidden-features-of-c-closed/…Waxplant
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 (stevedunns.blogspot.com/2010/04/summon-method.html)Slope
Funny side note, if you look up this property in reflector, you'll get a "this code has been obfuscated". At least the last time I tried (~ 6 months ago).Trilobate
L
27

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.

Lowis answered 12/8, 2008 at 16:32 Comment(7)
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: moffdub.wordpress.com/2008/07/01/method-regulator-patternCourtmartial
you can use <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> to help with this problem.Susurrate
@Maslow: Thanks, that helped me solve a problem and make our security system much easier to use.Thilda
@Courtmartial @Thilda 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 .NETMccrea
@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.Crabby
@Kallick 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.Mccrea
This is not really a language feature, but a framework featureImpulsion
D
27

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);
 }
Derbyshire answered 12/8, 2008 at 16:32 Comment(1)
See also stackoverflow.com/questions/271398/…Derbyshire
M
26

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.

Mulvey answered 12/8, 2008 at 16:32 Comment(1)
There is more to know about: https://mcmap.net/q/20118/-net-what-39-s-the-best-way-to-implement-a-quot-catch-all-exceptions-handler-quot implement-a-catch-all-exceptions-handlerCurriery
B
26

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

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

Bordelon answered 12/8, 2008 at 16:32 Comment(1)
It is still bound by the inaccuracies of floating point logic. If you really want an accurate modulus when using a decimal number, you should use Decimal.Pore
B
26

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.

Balaam answered 12/8, 2008 at 16:32 Comment(3)
+1 nice for reducing "progress update" slowdownsYearwood
Definitely sounds useful for building services or command line utilities.Meaghan
@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.Velamen
O
26

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...)

Oza answered 12/8, 2008 at 16:32 Comment(2)
+1 - you may be interested to know this seems to be used in the MVVMLight Toolkit in the Message Passing part.Gloxinia
the juanformoso.com.ar/post/2008/07/30/Weak-References.aspx link is dead.Jimmie
H
25

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());
Histiocyte answered 12/8, 2008 at 16:32 Comment(3)
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()Berhley
I fixed the errors. My point is that I see the former piece of code more than I find the laterHistiocyte
In .Net 4 String.Join works with IEnumerable so you don't need to convert to an array first.Langille
A
25

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).

Amah answered 12/8, 2008 at 16:32 Comment(3)
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.Sparky
[Conditional("DEBUG")]-marked methods usually make for cleaner, easier to read code.Parapodium
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!Hepatitis
W
25

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;
Wellspoken answered 12/8, 2008 at 16:32 Comment(3)
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.Exodontics
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.Prut
Thanks for this! I have always used ? the traditional way as mystring = foo.IsNullOrEmpty() ? "foo was null" : foo; since the time I first read about ternary operators and thought I was so cool! Until, now.. :PTroika
S
24

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));
}}
Susurrant answered 12/8, 2008 at 16:32 Comment(5)
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.Goldbrick
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.Susurrant
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.Susurrant
Interesting, but what is it good for?Whiskey
@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). bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx also check this blog postSusurrant
U
24

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?

Underbred answered 12/8, 2008 at 16:32 Comment(6)
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!Sybille
nice, never thought of that. Can be useful in some cases...Chemotherapy
One thing I didn't get is when false operator is called.Unduly
You can use this operators to shorten the null check syntax: class MyClass { public static bool operator true(MyClass o) { return o != null; } public static bool operator false(MyClass o) { return o == null; } public static bool operator !(MyClass o) { return o ? false : true; } }Unduly
I've seen some libraries that use this feature. Personally, I can find a few good uses, but I still like to express the condition explicitely, because the question "Is bannana" does not explain the purpose, while "Does bannana exist" (bannana != null) does explain the condition. Especially if you are writing the code that other will use.Lusty
@HuBeZa: see stackoverflow.com/questions/33265/…Laura
R
24

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)
Reborn answered 12/8, 2008 at 16:32 Comment(5)
I dunno, Partial methods have always seemed like a code smell to me...Smoky
In a lot of cases perhaps. Good for separating generated code from developer code though.Eyra
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.Abbyabbye
I use it just the way BlackWasp does;PartialMethod++ == Delegate-- == Speed * 2;Landholder
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.Harrar
P
23

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.

Pardon answered 12/8, 2008 at 16:32 Comment(2)
and why not just put 'Where T : struct' to avoid boxing...Breaker
@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.Pardon
H
22

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,
};
Heartstrings answered 12/8, 2008 at 16:32 Comment(4)
The comma at the end makes fiddling with the values much easier :)Myrmeco
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. :)Adman
Enums also support the trailing comma "feature".Meaghan
You don't need the () in Rectangle() eitherHairball
P
21

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

Parlin answered 12/8, 2008 at 16:32 Comment(3)
You don't need the temp variable. It's used for avoiding an async change, but the event parameter is sufficient for that.Southern
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.Parlin
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.Ubiquitarian
S
21

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.

Susuable answered 12/8, 2008 at 16:32 Comment(6)
I used this trick once, and it ended up being useless because Win32 does something to restore the cursor for you, at least in WinFormsIrmine
I've just declared class called DisposableAction that takes action in constructor and executes it on the disposal. Really convenient. build.lokad.com/doc/shared/html/E5642EA0.htmConvalescence
It is a bit cleaner than the try-finally approach that I find myself using.Adman
This is a useful way to implement certain internal DSLs. You can change the context of a bunch of declarative statements by encapsulating them in a using block.Gloxinia
Is it just me, or there is an extra semicolon?Ite
You're right - there is an extra semicolon - it's taken nearly three years for someone to spot!Susuable
M
21

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.

Manado answered 12/8, 2008 at 16:32 Comment(2)
Yes BeginInvoke/EnInvoke uses the threadpool, I learned that in this forum. See stackoverflow.com/questions/442991/… and msdn.microsoft.com/en-us/library/2e08f6yc.aspx .Leonidaleonidas
"things you didn't know about C# until recently despite thinking you already knew everything" +1Doldrums
K
20
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.

Kemberlykemble answered 12/8, 2008 at 16:32 Comment(13)
Isn't "" shorter than string.Empty?Paid
@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).Disqualification
@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.Intervention
Not exactly hidden if you know a little bit about the framework.Rubio
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.Luong
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 "".Splendiferous
I always found it a bit weird to be able to do typename.property in C#Saphena
bobobobo: static properties weird?Rubio
I find blogs.msdn.com/b/ericlippert/archive/2009/09/28/… to be of interest. It points out a case where "" and string.Empty will not have the same behavior during == comparisons.Body
@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) {...};Meistersinger
String.Empty is .NET, not C#. It is accessible from any syntax on the .NET framework.Mcclain
@Jeroen Pluimers - That's exactly why I use itCandiot
Where is the study that shows the statistically higher error rate in apps that use "" instead of string.Empty? They produce the same IL, one is shorter and requires a lot less typing. The suggestion that it is possible to insert a space between those double quotes is fanciful. If you have a reason for using a string consisting of 1 or more spaces you would obviously declare it as a const or readonly to avoid having magic values in your code.Langille
Y
20

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)

Yell answered 12/8, 2008 at 16:32 Comment(0)
S
20

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.

Stadium answered 12/8, 2008 at 16:32 Comment(0)
W
20

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.

Wadai answered 12/8, 2008 at 16:32 Comment(1)
Here's a link to a snippet that makes it easy to create a custom EventArgs classes and an event that uses it: stackoverflow.com/questions/1157072/…Adcock
R
20

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.

Rubio answered 12/8, 2008 at 16:32 Comment(0)
T
20

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.

Towner answered 12/8, 2008 at 16:32 Comment(0)
E
20

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


Parallel Sample :
In your opinion, how many CLR object can be created in one second? enter image description here
See fallowing example :

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace ObjectInitSpeedTest
{
   class Program
   {
       //Note: don't forget to build it in Release mode.
       static void Main()
       {
           normalSpeedTest();           
           parallelSpeedTest();

           Console.ForegroundColor = ConsoleColor.White;
           Console.WriteLine("Press a key ...");
           Console.ReadKey();
       }

       private static void parallelSpeedTest()
       {
           Console.ForegroundColor = ConsoleColor.Yellow;
           Console.WriteLine("parallelSpeedTest");

           long totalObjectsCreated = 0;
           long totalElapsedTime = 0;

           var tasks = new List<Task>();
           var processorCount = Environment.ProcessorCount;

           Console.WriteLine("Running on {0} cores", processorCount);

           for (var t = 0; t < processorCount; t++)
           {
               tasks.Add(Task.Factory.StartNew(
               () =>
               {
                   const int reps = 1000000000;
                   var sp = Stopwatch.StartNew();
                   for (var j = 0; j < reps; ++j)
                   {
                       new object();
                   }
                   sp.Stop();

                   Interlocked.Add(ref totalObjectsCreated, reps);
                   Interlocked.Add(ref totalElapsedTime, sp.ElapsedMilliseconds);
               }
               ));
           }

           // let's complete all the tasks
           Task.WaitAll(tasks.ToArray());

           Console.WriteLine("Created {0:N} objects in 1 sec\n", (totalObjectsCreated / (totalElapsedTime / processorCount)) * 1000);
       }

       private static void normalSpeedTest()
       {
           Console.ForegroundColor = ConsoleColor.Green;
           Console.WriteLine("normalSpeedTest");

           const int reps = 1000000000;
           var sp = Stopwatch.StartNew();
           sp.Start();
           for (var j = 0; j < reps; ++j)
           {
               new object();
           }
           sp.Stop();

           Console.WriteLine("Created {0:N} objects in 1 sec\n", (reps / sp.ElapsedMilliseconds) * 1000);
       }
   }
}
Enumeration answered 12/8, 2008 at 16:32 Comment(1)
It is not really a language feature, though, as it is just a library which uses other language features cleverly.Tradesfolk
S
19

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 
}
Slope answered 12/8, 2008 at 16:32 Comment(11)
Can you explain this? Not sure I follow how this works or what it doesCandiot
It's just shifting the bits in the binary representation of the number, 0001 << 1 becomes 0010, 0001 << 2 becomes 0100, 0001 << 3 becomes 1000Rancorous
Nice one. @burnt_hand it's the shift operator msdn.microsoft.com/en-us/library/aa691377%28v=VS.71%29.aspx. (shifts left-hand value left by the right-hand number of bits)Minify
(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 :)Fluor
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.Spreadeagle
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 ;)Ridden
@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!Slope
You're leaving one out, since 1 << 1 is 2, your first flag should be 1 << 0.Rubio
@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.Candiot
Is this really clearer than 0, 1, 2, 4, 8, 16...?Adcock
The "You're leaving one out, since 1 << 1 is 2" comment and subsequent fix points out an irony of human thought. While the shifting approach is in theory cleaner and safer because the computer does the math, it is in practice more error prone: the off-by-one error can be hard to spot, but an incorrect power of two sequence is easy.Farfamed
D
19

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 */      
    });

 }
Derbyshire answered 12/8, 2008 at 16:32 Comment(0)
G
19

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

Godesberg answered 12/8, 2008 at 16:32 Comment(4)
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.Myrmeco
@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.Velamen
@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.Lacustrine
I do this: var x = Nullable<X> ?? MyPreferedDefaultValue;Homebrew
C
18

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+.

Commemoration answered 12/8, 2008 at 16:32 Comment(2)
Yeah, great for test assemblies... And only test assemblies. Right? ;)Thilda
@Thilda - Yes, I would agree. There is a good reason for having the internal keyword, and that is to keep things internal :)Commemoration
G
17

Need to return an empty IEnumerable?

public IEnumerable<T> GetEnumerator(){
  yield break;
}
Gaussmeter answered 12/8, 2008 at 16:32 Comment(5)
Already in the BCL System.Linq.Enumerable.Empty<T>()Yearwood
@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...Gaussmeter
This one is a little opaque for my tastes, I prefer chakrit's solution.Dot
Actually, it's a wrapper around a static instance of T[], so new T[0] is only called once per type.Ashtoreth
@Alex: I'd prefer yield break in a generator function (i.e. which also yield returns) I'd use Enumerable.Empty everywhere elseGolub
M
17

You can limit the life and thus scope of variables by using { } brackets.

{
    string test2 = "3";
    Console.Write(test2);
}

Console.Write(test2); //compile error

test2 only lives within the brackets.

Moiety answered 12/8, 2008 at 16:32 Comment(3)
This is true of C++ too.Maskanonge
True of any language with lexical scoping no?Plaice
hmmm...Is this a hidden feature??Alsatia
U
17

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

Underbred answered 12/8, 2008 at 16:32 Comment(0)
V
17

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";
Vennieveno answered 12/8, 2008 at 16:32 Comment(6)
I use '@' all the time, but I never thought of spanning multiple lines! :-PMeris
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.Eyra
Another problem is that you're writing SQL in code :(Gun
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.Vennieveno
The SQL uses parameters. Apart from the SQL itself being hardcoded, I don't see any problems.Rubio
Agree with Dave. Why it is "always" bad to write sql in code? What about hardcode complete statements. I see no problems too.Carden
G
16

You can store colors in Enum.

public enum MyEnumColors : uint
{
    Normal          = 0xFF9F9F9F,
    Active          = 0xFF8EA98A,
    Error           = 0xFFFF0000
}
Grantland answered 12/8, 2008 at 16:32 Comment(2)
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.Varrian
I think FFire is trying to draw attention to the fact that you can specify the base type (in this case uint) for an enumGolub
C
16

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#?

Convalescence answered 12/8, 2008 at 16:32 Comment(1)
+1 You can also do this with anonymous types.After
B
16

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!";
    }
}
Backstretch answered 12/8, 2008 at 16:32 Comment(4)
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.Backstretch
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.Myrmeco
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.Thilda
+1 - I'm using the unicode Pi and Tau symbols as members in my Math class - makes code much nicer to read! float area = Math.π * (radius * radius). What I'd really like to do is define an extension method called ² so I can type float area = Math.π * radius.²();After
M
15

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.

Mulvey answered 12/8, 2008 at 16:32 Comment(6)
I think this should include an example of why/how this can be usefulInordinate
@Toein 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.Mulvey
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 :-)Iggie
@Iggie code formating is removed. Hope it is easy to notice links now.Mulvey
What has not been mentioned so far, for people that started learning programming with a GC: stack allocation is basically just an incrementation of the stack pointer and is therefore, well, let's call it "really fast". ;-) since there will be a stack allocated for the running function, anyways, i guess that the stackalloc memory is just added when doing that allocation, therefore making it a no-op without any runtime penalties at all. That's just my guess, though.Zeller
IMO, only including links to explain somthing can limit the usefulness of your answer, as the links may eventually become broken.Huss
M
15

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.

Mulvey answered 12/8, 2008 at 16:32 Comment(1)
Hmm... could that make aborting threads safe if they were written for it?Verulamium
F
15

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.
 }
Feoffee answered 12/8, 2008 at 16:32 Comment(3)
it must be 32 i think not 31, what you say?Mattins
Remember that it is a flagged enum. So in this case "All" equal to (Red & Blue & White & Black & Green) then it would also equal to (00001 & 00010 & 00100 & 01000 & 10000) and equal to 11111 and equal to 31 as well. The number 32 should be 100000 but 11111.Feoffee
I prefer spelling it out All = Red | Blue | White | Black | Green and e.g. Dark = Blue | Black, Light = All & ~DarkGolub
T
15

You can add and remove delegates with less typing.

Usual way:

handler += new EventHandler(func);

Less typing way:

handler += func;
Taipan answered 12/8, 2008 at 16:32 Comment(3)
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.Yearwood
@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.Telegram
@Aneves: Resharper lets you easily remove this extra bit of fluff though.Disqualification
V
15

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.

Violence answered 12/8, 2008 at 16:32 Comment(3)
I think you can do bitwise operations on any enum, flags only affect the ToString() methodIrmine
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...Vange
You cannot use the Enum.HasFlag(...) method unless the Enum you're referencing has the FlagsAttribute attribute.Respect
A
15

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.

I am really just saying that generics can be an enormously powerful beast in the wrong hands.

Augend answered 12/8, 2008 at 16:32 Comment(4)
Maybe I'm being daft, but I'm not sure I understand the code. Could you post a more complete example?Orderly
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.Dorseydorsiferous
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?Connivent
No, the little trick is constraining to BaseParameterType. A bit of a typo :\Dorseydorsiferous
B
14

You can put several attributes in one pair of square brackets:

    [OperationContract, ServiceKnownType(typeof(Prism)), ServiceKnownType(typeof(Cuboid))]
    Shape GetShape();
Bigg answered 12/8, 2008 at 16:32 Comment(1)
I hate it when CodeAnalysis puts it SuppressMessage attribute inside other attributes... For me I like to allign all attributes.Trapeze
A
14

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);
        }
    }
}
Archespore answered 12/8, 2008 at 16:32 Comment(0)
I
14

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 {}
Impulsion answered 12/8, 2008 at 16:32 Comment(1)
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.Adcock
L
14

Labeling my endregions...

#region stuff1
 #region stuff1a
 //...
 #endregion stuff1a
#endregion stuff1
Laurinelaurita answered 12/8, 2008 at 16:32 Comment(0)
D
14

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 :-)

Dayna answered 12/8, 2008 at 16:32 Comment(2)
It applies Curiosity as an attribute to the returned instance.Dayna
You can also use attributes on method parameters (common in MVC) eg. public ActionResult Register([Bind(Prefix = "Person")] string name)Calvert
B
14

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.

Bollard answered 12/8, 2008 at 16:32 Comment(0)
F
13

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.

Fractionate answered 12/8, 2008 at 16:32 Comment(5)
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.Adcock
+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.Crabby
@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.Crabby
@Kallick Burns: thanks for the revision. It's now even more easy on the eyes :)Overindulge
You can even use the Field<T>(name) extensions: r.Field<int>("MessageId") (I wish I didn't have to know that)Dusty
J
13

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!

Jayson answered 12/8, 2008 at 16:32 Comment(0)
F
13

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");
}
Flemings answered 12/8, 2008 at 16:32 Comment(8)
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.Suboceanic
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.Flemings
@John: I tried Brian's code, it works.Fabi
@Brian: What happens, when input is null? I´m not so familiar with extension methods.Archespore
@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.Berceuse
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()Minify
@John I think it works because the original IsNullOrEmpty take one parameter and not this one. So it's just overloading.Minify
@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.Suboceanic
F
13

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
Falange answered 12/8, 2008 at 16:32 Comment(1)
+1 for explicitely showcasing the ToString() behaviour for combined flagsGolub
U
13

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".

Uralic answered 12/8, 2008 at 16:32 Comment(0)
I
13

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

Interdict answered 12/8, 2008 at 16:32 Comment(0)
D
13

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] );             
}
Dwt answered 12/8, 2008 at 16:32 Comment(1)
Lambda Expressions are explained more fully here: developer.com/net/csharp/article.php/3598381 and here: msdn.microsoft.com/en-us/library/bb397687.aspxOrdinand
G
12

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
    {
        // ...
    }
}
Graphomotor answered 12/8, 2008 at 16:32 Comment(0)
H
12

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

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

instead of doing:

"string".ToLower() == "String".ToLower();
Hypnogenesis answered 12/8, 2008 at 16:32 Comment(4)
I know this should be the correct approach, but I'm annoyed by verbosity of the enum itself.Fico
Actualy, StringComparison.OrdinalIgnoreCase should be used in many case.Herculaneum
also microsoft has optimized for upper case comparisons so ToUppper would be better on the old way.Susurrate
This is the correct way to compare strings.Acrimony
C
12

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;
}
Chantay answered 12/8, 2008 at 16:32 Comment(7)
Note that "goto case" is discouraged in this article: msdn.microsoft.com/en-us/vcsharp/aa336815.aspxUpanishad
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.Chantay
I think in a switch is only place a goto is acceptable.Gun
I think I would be very upset if I encountered code like this and had to debug or maintain it...Watchcase
@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.Chantay
case 2: default: break; will return the same thing even without the goto default;Valuate
@Martin Ongtangco: Only if case 2's "// do something else" is empty.Chantay
C
11

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.

Crystallization answered 12/8, 2008 at 16:32 Comment(2)
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.Airsickness
What's even better -- ToStringOrDefault(this object obj, object default) -- and ToStringOrEmpty(this object obj) { obj.ToStringOrDefault(String.Empty); }Acrimony
L
11

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.

Leonidaleonidas answered 12/8, 2008 at 16:32 Comment(4)
In this case, would you need to do var a = (IPropA)t; var propa = a.PropA; Just in case the interface is explicitly implemented?Tressa
@Mark, I don't understand your question so I can't answer it.Leonidaleonidas
@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.Velamen
@tuinstoel: +1! This is an excellent demonstration of functionality that's not possible without generics.Velamen
A
11

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.

Arleanarlee answered 12/8, 2008 at 16:32 Comment(2)
Action<T> is available in .NET 2.0. It's a shame that Func isn't, because it would make using lambdas easier.Myrmeco
A few of them: Action, Comparison, Converter, EventHandler, Func, Predicate (see stackoverflow.com/questions/319789/… )Leasia
S
11

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#.

Smother answered 12/8, 2008 at 16:32 Comment(0)
R
11
  1. I can't comment yet, but note that by default Visual Studio 2008 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 (useful 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.

  1. 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).

Rooftree answered 12/8, 2008 at 16:32 Comment(0)
C
11

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

Calm answered 12/8, 2008 at 16:32 Comment(0)
J
10

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
Jeminah answered 12/8, 2008 at 16:32 Comment(0)
A
10

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.

Aglitter answered 12/8, 2008 at 16:32 Comment(0)
G
10

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.

Gitt answered 12/8, 2008 at 16:32 Comment(1)
"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." this means "x=y" ^^ EDIT: didn't notice the huge necromancy i just performed. oh well.Homes
I
10

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 :)

Indo answered 12/8, 2008 at 16:32 Comment(0)
N
10

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.

Nidianidicolous answered 12/8, 2008 at 16:32 Comment(4)
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.Declarative
Can't you just use TODO: comments in Visual Studio? dotnetperls.com/todo-comments-visual-studioCalvert
@DanDiplo Not everyone's using Visual Studio...Champaigne
@DanDiplo you can. And you can also use #error and #warning. There is a time and a place for everything. Consider if you're using other preprocessor directives, and there happens to be a set that is either untested, or known to break things... you can put #errors and #warnings in those cases (counter-respectively), and you'll never see them unless someone tries to compile with improper settings; (ex: You're using PInvoke and the call doesn't work if you compile in 64-bit mode, use preprocessor directive to detect that you're compiling for any-CPU and include an #error.)Acrimony
A
10

TrueForAll Method of List<T> :

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

bool a = s.TrueForAll(p => p > 0);
Arthromere answered 12/8, 2008 at 16:32 Comment(1)
Linq: s.All(p => p > 0);Carden
S
10

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()

Sludgy answered 12/8, 2008 at 16:32 Comment(0)
W
9

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();
Watchcase answered 12/8, 2008 at 16:32 Comment(1)
int totalItems = Orders.SelectMany(o => o.LineItems).Sum();Susurrant
H
9

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.

Hungry answered 12/8, 2008 at 16:32 Comment(1)
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.Graphomotor
M
9
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.

Mastiff answered 12/8, 2008 at 16:32 Comment(4)
don't these result in some pretty unclear message boxes being display to a user if one of these happens in production?Susurrate
Well, yes, that's why you should #ifdef them out and/or remove them from production builds.Mastiff
@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...Parapodium
use System.Diagnostics.Debugger.Launch()Selhorst
E
9

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

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

Euphemie answered 12/8, 2008 at 16:32 Comment(1)
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!Sadfaced
M
3

Lately I learned about the String.Join method. It is really useful when building strings like columns to select by a query.

Mickens answered 12/8, 2008 at 16:32 Comment(4)
That's in the .NET Framework (not in C# itself) and is far from hidden, actually. ;-)Monad
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.Mickens
Isn't that the same as String.Concat ?Tsingyuan
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.Mickens
S
3

With reference to the post w/ perma link "Hidden Features of C#?", 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!

Swatter answered 12/8, 2008 at 16:32 Comment(1)
A language feature it certainly is notGolub
F
3

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

Floatplane answered 12/8, 2008 at 16:32 Comment(0)
B
2

Most of the P/Invoke stuff is a bit strange.

Example of attributes:

[DllImport ("gdi32.dll")] 
[return : MarshalAs(UnmanagedType.I4)]
[StructLayout(LayoutKind.Sequential)]
Boilermaker answered 12/8, 2008 at 16:32 Comment(0)
T
2

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

Testimony answered 12/8, 2008 at 16:32 Comment(0)
M
2

Another way of geting IEnumerable through yield without explicity creating an IEnumerable object

public IEnumerable<Request> SomeMethod(IEnumerable<Request> requests)
{
    foreach (Request request in requests)
       yield return DoSomthing(request);
}
Mikol answered 12/8, 2008 at 16:32 Comment(1)
of course the sample is weak, and it is hardly hidden. I'd prefer to write this as requests.Select(DoSomthing) 10 out of 10 times. (Also, edited the sample to fix code errors)Golub
G
2

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\";

Geoffreygeoffry answered 12/8, 2008 at 16:32 Comment(0)
H
2

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.

Highness answered 12/8, 2008 at 16:32 Comment(1)
Hidden features are found by those that care about the fine details.Attractant
R
2

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.

Riviera answered 12/8, 2008 at 16:32 Comment(0)
A
2

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.

Alcott answered 12/8, 2008 at 16:32 Comment(0)
T
2

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 :)

Thoroughgoing answered 12/8, 2008 at 16:32 Comment(1)
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.Fa
N
2

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().

Nimocks answered 12/8, 2008 at 16:32 Comment(0)
C
2

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.

Carpal answered 12/8, 2008 at 16:32 Comment(0)
P
2

@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.

Paragon answered 12/8, 2008 at 16:32 Comment(0)
I
2

PreviousPage property:

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

It is very useful.

Incipient answered 12/8, 2008 at 16:32 Comment(1)
This is not a feature of C#, but rather of the System.Web.UI.Page class.Goldbrick
E
2

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
}
Eng answered 12/8, 2008 at 16:32 Comment(0)
H
2

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!

Halo answered 12/8, 2008 at 16:32 Comment(0)
H
1

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>
Hengel answered 12/8, 2008 at 16:32 Comment(3)
I can only see static constuctors. None of the classes are defined as static...Hesperidium
@sehe: My comment was related to the original opening line - 'You can have instances of static classes'. This was edited 40 mins after my comment, presumably in responseHesperidium
@MPritch: mmm. ok I'll delete my comment then as it is not helpful, perhaps you could do the same and no-one will even know we were here :)Golub
K
1

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));
}
Kallick answered 12/8, 2008 at 16:32 Comment(0)
B
1

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

Boak answered 12/8, 2008 at 16:32 Comment(1)
Sorry, but that is not a language feature. It is an software instrumentation method (akin to profiling, tracing, AOP weavers, verifiers etc). It is a side effect of having a structured VM instruction set (like Perl, Python, Java and CLR, to name a few)Golub
H
1

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.

Handel answered 12/8, 2008 at 16:32 Comment(0)
S
1

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 
Serrated answered 12/8, 2008 at 16:32 Comment(5)
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".Tressa
That happens when you use such an illegible construct.Tamalatamale
if statements are far more readable than that. This version simply gives you street cred :)Attractant
Just use an if statement man!Trespass
max street cred: ` var s = new SortedList<int, string> { { 2, "less than 2" }, { 5, "less than 5" }, { 10, "less than 10" } } .Where(kv => i < kv.Key) .Select(kv => kv.Value) .FirstOrDefault()?? "something else"; `Golub
C
1

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

Crescint answered 12/8, 2008 at 16:32 Comment(0)
T
1

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?

Trill answered 12/8, 2008 at 16:32 Comment(0)
R
1

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).

Risinger answered 12/8, 2008 at 16:32 Comment(0)
G
0

I don't know if this is a hidden feature (""). Any string function.

Galateah answered 12/8, 2008 at 16:32 Comment(4)
"".func() also works but what good use can we possibly put it to?Carlock
Just a quick shortcut for the non static functionsGalateah
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. :-)Appraisal
@pretezel wait, what? wouldn't that exception unless SomeMethod() was an extension method?Devest

© 2022 - 2024 — McMap. All rights reserved.