C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } … } I should elaborate – the reason those expressions in your example are not compile-time constants is because at compile time, the compiler can’t simply execute TimeSpan.FromSeconds(2.0) and … Read more

.NET NewtonSoft JSON deserialize map to a different property name

Json.NET – Newtonsoft has a JsonPropertyAttribute which allows you to specify the name of a JSON property, so your code should be: public class TeamScore { [JsonProperty(“eighty_min_score”)] public string EightyMinScore { get; set; } [JsonProperty(“home_or_away”)] public string HomeOrAway { get; set; } [JsonProperty(“score “)] public string Score { get; set; } [JsonProperty(“team_id”)] public string TeamId … Read more

How do I get the current line number?

In .NET 4.5 / C# 5, you can get the compiler to do this work for you, by writing a utility method that uses the new caller attributes: using System.Runtime.CompilerServices; static void SomeMethodSomewhere() { ShowMessage(“Boo”); } … static void ShowMessage(string message, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { MessageBox.Show(message + ” … Read more

How do I check if a property exists on a dynamic anonymous type in c#?

public static bool DoesPropertyExist(dynamic settings, string name) { if (settings is ExpandoObject) return ((IDictionary<string, object>)settings).ContainsKey(name); return settings.GetType().GetProperty(name) != null; } var settings = new {Filename = @”c:\temp\q.txt”}; Console.WriteLine(DoesPropertyExist(settings, “Filename”)); Console.WriteLine(DoesPropertyExist(settings, “Size”)); Output: True False

Why covariance and contravariance do not support value type

Basically, variance applies when the CLR can ensure that it doesn’t need to make any representational change to the values. References all look the same – so you can use an IEnumerable<string> as an IEnumerable<object> without any change in representation; the native code itself doesn’t need to know what you’re doing with the values at … Read more

How does the C# compiler detect COM types?

By no means am I an expert in this, but I stumbled recently on what I think you want: the CoClass attribute class. [System.Runtime.InteropServices.CoClass(typeof(Test))] public interface Dummy { } A coclass supplies concrete implementation(s) of one or more interfaces. In COM, such concrete implementations can be written in any programming language that supports COM component … Read more

Difference between Covariance & Contra-variance

The question is “what is the difference between covariance and contravariance?” Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set. Consider the following two subsets of the set of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)