Browsing 7239 questions and answers with Jon Skeet

C# Converting object to int without casting

I have a method that returns a type of object based on the given column (a database class). However, when I assign the object the compiler throws an error saying it cannot...
Jon Skeet
people
quotationmark

You could change your indexer signature to: public dynamic this[string name] That would then make the conversion dynamic at execution time. Personally, I prefer the cast approach though. It makes it clear that this is something that... more 3/21/2015 7:47:58 AM

people

Default case in toString for enums

I am somewhat new to enums in Java, and I am trying to override toString() so that it can return special cases for an enum with creating code for each case: enum TestEnum { One,...
Jon Skeet
people
quotationmark

Is there a way to get around this and return any cases not included in the toString()? I think you just want: return super.toString(); Then it won't call itself - it'll just use the superclass implementation. However, I'd change... more 3/21/2015 6:56:54 AM

people

yield return memory optimization

And yet another question about yield return So I need to execute remotely different SQL scripts. The scripts are in TFS so I get them from TFS automatically and the process...
Jon Skeet
people
quotationmark

Once that I'm iterating them, what happens with the script variable used in the foreach loop after it goes out of scope? Is that disposed? or does it remain in memory? If you mean in the ExecuteScripts method - there's nothing to... more 3/20/2015 6:10:21 PM

people

Return IQueryable<T> as IEnumerable<T> will cause in database call?

I use Entity Framework. Let's say I have a method: public IEnumerable<User> GetUsers() // return IEnumerable<User> { using (var context = new AppDbContext()) ...
Jon Skeet
people
quotationmark

Not if you don't do anything with it, no. However, if you try to iterate over the results (or call Count() etc) then it will try to make a database call... and I'd expect it to then fail, because you've disposed of the context at that... more 3/20/2015 6:01:46 PM

people

Why doesn't incrementing Nullable<int> throw an exception?

Could you please explain, why does Console.WriteLine write empty line (Console.WriteLine(null) give me compilation error) and why there isn't NullReferenceException (even a+=1...
Jon Skeet
people
quotationmark

You're observing the effects of a lifted operator. From section 7.3.7 of the C# 5 specification: Lifted operators permit predefined and user-defined operators that operate on non-nullable value types to also be used with nullable... more 3/20/2015 5:38:36 PM

people

C# SQLite syntax error (am I blind?)

I use SQLite as my database engine and my program should insert a new user into my database. But it gives me the error: SQL logic error or missing database near "group": syntax...
Jon Skeet
people
quotationmark

group is a keyword in SQLite, so you need to escape it. Any of the following should work: "INSERT INTO usersDb (uid, agentname, `group`, ...)" "INSERT INTO usersDb (uid, agentname, [group], ...)" "INSERT INTO usersDb (uid, agentname,... more 3/20/2015 3:54:14 PM

people

Exception creating JSON with LINQ

I am trying to create JSON with the following code: JArray jInner = new JArray("document"); JProperty jTitle = new JProperty("title", category); JProperty...
Jon Skeet
people
quotationmark

It doesn't make sense to add a property to an array. An array consists of values, not key/value pairs. If you want something like this: [ { "title": "foo", "description": "bar" } ] then you just need an intermediate... more 3/20/2015 2:22:19 PM

people

Why isn't the static constructor hit first?

I have the below code and I have only two simple questions which are normal to expect in the behavior of the program below, which I am not seeing unfortunately: Static...
Jon Skeet
people
quotationmark

This is the problem: public static B b = new B(); Static field initializers are executed before the static constructor is executed. From the C# spec section 10.5.5.1: If a static constructor (10.12) exists in the class, execution of... more 3/20/2015 1:29:52 PM

people

Is there a type safe way to reference a class/property in an attribute?

Let's say I have a class, PersonAddressValidator that takes in a person object and an address object and does some validation (this isn't really the problem I am modelling so bear...
Jon Skeet
people
quotationmark

There's nothing to make it absolutely safe at compile-time from a C# language perspective, no. There are things you can do to help though. In C# 6, you can use the nameof operator, which makes it refactoring-friendly and makes it more... more 3/20/2015 1:26:02 PM

people

Distinguish between multiple optional arguments in c# Console App

I've read a number of articles on SoF about this, but funny enough, I feel that there is not concise answer. Maybe I just haven't found the correct post yet. I want to know the...
Jon Skeet
people
quotationmark

Yes, you need to provide some way of saying which value is which - just like you would for named arguments in a method call. The common conventions are: --myArg4=value4 --myArg4 value4 (Some tools prefer --, others prefer -.) There... more 3/20/2015 1:00:06 PM

people