Captured variable in a loop in C#

Captured variable in a loop in C#

I met an interesting issue about C#. I have code like below. List<Func<int>> actions=new List<Func<int>>(); int variable=0; while(variable<5){actions.Add(()=> variable*2);++ vari…


Explicit vs implicit call of toString

I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception. For instance: System.out.println("obj…


Are enums only named integers, types or neither of both?

Fun with enums in C#. Take one generic list that is created to store some Enum that you had defined previously and add few items in it. Iterate with foreach or GetEnumerator<T>() but specify some oth…


c# - Cannot implicitly convert List<T> to Collection<T>

This is a compiler error(slightly changed for readability). This one always puzzled me. FxCop tells that this is a bad thing to return List and classes that are\derived from Collection<T> should be p…


Best practices for DataBinding in asp.net for maintainability

I would like to know what are the best practices for using asp.net DataBinding, in terms of maintainability. I don't want the application to fall appart when I have to make changes to the database.…


Can I avoid casting an enum value when I try to use or return it?

If I have the following enum: public enum ReturnValue{ Success=0, FailReason1=1, FailReason2=2//Etc...}Can I avoid casting when I return, like this: public static int main(st…



c# - Using implicitly typed local variables

This question already has an answer here: Why would var be a bad thing? 17 answers I just installed a trial version of ReSharper and one of the first thin…


Cannot implicitly convert type 'X' to 'string'-when and how it decides that it “cannot”?

Right now I'm having it with Guids. I certainly remember that throughout the code in some places this implicit conversion works, in others it does not. Until now I fail to see the pattern. How the co…


linq to sql - C# Linq-to-Sql-Should DataContext be disposed using IDisposable

I have several methods that deal with DB and all of them start by calling FaierDbDataContext db=new FaierDbDataContext(); Since the Linq2Sql DataContext object implements IDisposable, should this b…


What's the point in defaulting functions in C++11?

C++11 adds the ability for telling the compiler to create a default implementation of any of the special member functions. While I can see the value of deleting a function, where's the value of explic…




type cannot convert string object how example system implicit constructor