Browsing 15901 questions and answers with Jon Skeet

how to get property type to assign null values

Basically, I am in need of a converter which converts a DataReader object to generic type. so when I do - while(dataReader.HasRows()){ var result=...
Jon Skeet
people
quotationmark

You can't specify null as a value of type T because it might not be a valid value for T. (Think int etc.) However, you can use default(T), which will be the default value for T - the same value you'd get if you left a field of type T... more 9/8/2017 7:10:15 AM

people

Calculate week numbers with year starting in April

I am trying to calculate week numbers depending on specific rules. The example of the rule I am looking for is: Week 1 = first week of the year which containers 1st...
Jon Skeet
people
quotationmark

How I'd approach this would depend on exactly what you need to do. In Noda Time 2.0 (and backported to 1.4) there's an IWeekYearRule interface you could implement, if you want to do this thoroughly. If you just need to compute the year... more 9/7/2017 8:53:54 AM

people

Parsing Decimal to DateTime in multiple date formats with a leading zero stripped out?

So I think there is a simple answer to this problem. Essentially I am getting a decimal storage of a DateTime from a database that is in essence '9062017'. I was wanting to...
Jon Skeet
people
quotationmark

I think you should avoid the string conversion entirely. It's pointless and error prone. It's much simpler just to do the maths: public static DateTime Int32ToDateTime(int value) { int year = value % 10000; int day = (value /... more 9/6/2017 5:42:02 PM

people

Is it possible to give a custom build error with T4?

So something like <# GiveError("Something is not right"); #> which would then show as compilation error in the error list among the other compilation errors. This could...
Jon Skeet
people
quotationmark

I don't know anything about T4 generation specifically, so I can't help in terms of halting the generation process with a useful message, but you may find it's better just to generate a #error pragma, e.g #error No configuration found for... more 9/6/2017 8:10:11 AM

people

How to define this 'var' type formally?

This code: var items = query.ToList(); returns List<'a> where 'a is new { string a1, string a2 } If I were to remove the var keyword, what would be a valid type...
Jon Skeet
people
quotationmark

Nothing, because it's a list of an anonymous type. The very term "anonymous type" was chosen because the type doesn't have a name. If you want to be able to use explicit typing, don't use an anonymous type. Either create your own type, or... more 9/5/2017 1:41:44 PM

people

Difference in C# between different getter styles

I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if...
Jon Skeet
people
quotationmark

Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor, just like a regular... more 9/5/2017 5:46:18 AM

people

How to implement server side google authorization on dotnet core

I need to implement server-side validation for in-app purchases of my android application, in C# dotnet core platform which runs on linux. I have read this page so many times and...
Jon Skeet
people
quotationmark

The issue you linked to is about users logging in via MVC. If you want to use a service account: If your server is on Google Cloud (e.g. Compute Engine, Container Engine or App Engine) you shouldn't need to do anything Otherwise,... more 9/4/2017 2:57:41 PM

people

Simplifying a foreach loop with LINQ (selecting two objects in each iteration)

Given the following interface and two classes: public interface IMyObj { int Id { get; set; } } public class MyObj1 : IMyObj { public MyObj1(int id) { Id = id; } ...
Jon Skeet
people
quotationmark

SelectMany is indeed what you want: var list = numbers.SelectMany(n => new IMyObj[] { new MyObj1(n), new MyObj2(n) }) .ToList(); In other words, for each number, create a two-element array, which is then flattened... more 8/30/2017 12:51:00 PM

people

java.lang.NumberFormatException StopWatch.roundTwoDecimals

I am new to Stack Overflow, I'm looking forward to you guys helping me out. I am new to programming and software development, specifically Android development. I developed a...
Jon Skeet
people
quotationmark

Double.valueOf expects a value formatted with a dot as a decimal point, always. You're creating a DecimalFormat that uses the system default locale, and the . there is the locale-specific decimal separator. The smallest change that might... more 8/30/2017 6:17:02 AM

people

Invalid variance with delegate in interface method

Why does the following interface declaration produce an invalid variance error? public interface ICache<in TKey, TValue> { TValue GetOrAdd(TKey key, Func<TKey,...
Jon Skeet
people
quotationmark

TL;DR: The C# compiler is keeping you safe. Effectively, "nested input in an input position makes an output" when it comes to generic variance. Your method has a parameter which itself accepts a TKey, which sort of reverses the... more 8/29/2017 5:33:04 PM

people