Browsing 7239 questions and answers with Jon Skeet

C# Set default namespace when conflicted

Is it possible to set a default namespace for the compiler to look at when you have a conflict between two namespaces? My problem comes from using iTextSharp in a C# ASP.NET...
Jon Skeet
people
quotationmark

Well not quite like that, but you could use a namespace alias directive: using System.Web.UI.WebControls; using text = iTextSharp; ... text::ListItem l = new text::ListItem(); You'd then not want a straight using directive for... more 12/12/2014 12:33:12 PM

people

Can I write the method name without using a String to pass the name of the method? (c#)

I have got this class: class foo { int val; public int Val { set{ val = values; }, set{ val = values; } } } I need to pass the property name to a...
Jon Skeet
people
quotationmark

As of C# 6, you can use the nameof operator: ctrl.DataBindings.Add(nameof(foo.Val), /* other arguments as before */); Before C# 6, there's no really simple way to do this at compile-time. One option, however, is to have unit tests which... more 12/12/2014 10:41:35 AM

people

How to swallow ... exception with specific reason

In this method public static void Detach() { try { using (var master = new DataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;Initial...
Jon Skeet
people
quotationmark

You don't want to check the message - you want to check the SQL specific number. You can use the SqlException.Number property for this. I would use: // I think this is right, based on //... more 12/12/2014 10:29:09 AM

people

Why the interface references shows toString(), hashCode() and other Object’s method

If an interface does not extends Object class then why the interface references shows toString(), hashCode() and other Object’s method.
Jon Skeet
people
quotationmark

Because that's the way the language is designed. Any class implementing the interface will definitely have Object as an ultimate ancestor, so at execution time, those methods will definitely be available. This is specified in JLS 9.2: ... more 12/12/2014 9:21:23 AM

people

Calling a method and passing an object reference and catching the return with same reference

I have a beginners question. I searched a lot for the answer but can't seem to find the exact answer so maybe somebody of more experienced developers can help me with this...
Jon Skeet
people
quotationmark

In the scenario you've shown, there isn't much obvious benefit. However, as a pattern it can be useful in my experience - particularly if the method performs validation. Guava's Preconditions.checkNotNull method is a great example of... more 12/12/2014 9:20:00 AM

people

shortening if statement with same conditions

I'm comparing the boolB here twice, and this code looks like it needs improvement. Is it possible to write this different to reduce code repetition? if (boolA) { if (boolB) ...
Jon Skeet
people
quotationmark

Well, you could a conditional operator to make it clearer - at least to my eyes: return boolA && boolB ? "A" : boolA ? "B" : boolB ? "C" : "D"; Once you get used to this way of writing multiple conditional operators,... more 12/12/2014 8:59:35 AM

people

TargetParameterCountException: Parameter count mismatch on invoke delegate

I have the following code (removed unrelated) //top of class declaration private delegate void UpdateFormElements(string status, bool addEmptyRow); //inside a function in my...
Jon Skeet
people
quotationmark

It's not clear why you're trying to use an anonymous method here. The problem is that you're creating a delegate type with two parameters, but you're not passing arguments (values for those parameters) into Invoke. I suspect you just... more 12/12/2014 8:20:36 AM

people

C# Regex replace using Dictionary

I am looking for an option to replace multiple characters of a string in one go. I'm trying to pass a string as a parameter for a URL which has special characters like,"...
Jon Skeet
people
quotationmark

This has nothing to do with using a dictionary - it's simply because you're using characters with special meaning in regular expressions (+, ( and )) without quoting them. You'd get the same issue if you simply had value =... more 12/12/2014 7:06:13 AM

people

An unhandled exception of type 'System.StackOverflowException.Cannot evaluate expression because the current thread is in a stack overflow state

public class ConnectionElement : ConfigurationElement { [ConfigurationProperty("uri", DefaultValue = "/")] public String Uri { get {...
Jon Skeet
people
quotationmark

Both your getter and your setter are calling themselves. Remember that properties are just groups of methods, effectively. Your class really looks like this, but with a bit of extra metadata: public class ConnectionElement :... more 12/12/2014 6:56:09 AM

people

Find string in array to another string array C#

Give two string arrays I want to find the first occurrence of a string in A1 with A2. I know I can do it "long hand" but could I use Array.Find() or something like that? Many...
Jon Skeet
people
quotationmark

It sounds like you're interested in the intersection, basically. LINQ to the rescue! var firstCommon = a1.Intersect(a2).FirstOrDefault(); The documentation for Intersect would suggest this will return the items in the order of a2: ... more 12/11/2014 7:49:21 PM

people