Browsing 7239 questions and answers with Jon Skeet

Adding Optional parameter in ClassLibrary Does not Provide BackWord Compatibility

I am adding Optional Parameter in My ClassLibrary Example public static string GetData(string name, string surname, bool status = false){} And My ASPX page is calling...
Jon Skeet
people
quotationmark

If you really want binary compatibility then yes, you can't add even an optional parameter. What you can do is add an overload: Before: public static string GetData(string name, string surname) { // Real code } After: public... more 9/12/2016 9:59:23 AM

people

How many objects were created where B is inherited from A?

Here are two classes, class A { } class B extends A { } and A is inherited from B.What confuses me is that How many objects were created when I use the following code A a = ...
Jon Skeet
people
quotationmark

It creates a single object, which is an instance of B. It's already a B when the A constructor executes, as you'll see if you change your code to: class A { public A() { System.out.println(getClass()); } } class B extends... more 9/12/2016 7:08:32 AM

people

Pass by Reference vs Pass by value result

Call by reference makes an alias of the formal parameter so every change of typical parameter affects also formal parameter, while pass-by value- result copies the result to the...
Jon Skeet
people
quotationmark

It would make a difference if the original variable were read during the course of the method. This could happen because: Two parameters were both provided using the same underlying variable The method invoked more code that read from... more 9/11/2016 8:56:50 PM

people

Easy way to get subset of string[] in C#

In python I am trying to just do object[1:], skipping the first element and then returning the rest of the string[]. What is the best way to go about this?
Jon Skeet
people
quotationmark

Now would be a good time to learn about LINQ - you want Skip(1) to skip the first element. Then you can use ToArray to create an array if you really need to. For example: string[] original = { "a", "b", "c" }; IEnumerable<string>... more 9/10/2016 4:16:20 PM

people

What is best way to create converter from function?

I want to apply func object as converter like this: Func<int, double> f = x => x / 2.5; Converter<int, double> convert = f; List<double> applied_list = new...
Jon Skeet
people
quotationmark

There are three options here. In my preferred order, they are: Start using LINQ instead of ConvertAll: List<double> appliedList = new List { 1, 2, 3 }.Select(f).ToList(); Create a converter to start with: Converter<int,... more 9/9/2016 10:41:29 AM

people

Selecting List<string> into Dictionary with index

I have a List List<string> sList = new List<string>() { "a","b","c"}; And currently I am selecting this into a dictionary the following...
Jon Skeet
people
quotationmark

There's no ToDictionary method that gives this in a single call, as there are no overloads that provide the index, but you can use the Select overload which accepts a Func<T, int, TResult>: var dictionary = list.Select((value,... more 9/8/2016 8:43:44 AM

people

Array indexer signature returns object does it box?

I stumbled on the fact that the indexer this[int index] { get; } works differently for an array of structs than it does for a List of structs. Namely, that the indexer in the case...
Jon Skeet
people
quotationmark

Namely, that the indexer in the case of an T[] returns a reference to the element within the array Not really. It's more that there isn't an indexer for arrays - instead an element-access expression represents an array access instead... more 9/7/2016 3:08:49 PM

people

JS like enumaration in C#

In JavaScript I can have an array of different objects in each cell, and when enumerating it each cell will get treated as the object it is and not as an underlying common object...
Jon Skeet
people
quotationmark

Well, you can use dynamic typing if you really want: public class Paint { public string Color { get; set; } } public class Car { public string Color { get; set; } } ... var objects = new object[] { new Car { Color = "Red"... more 9/7/2016 9:24:18 AM

people

C# member method as static method ("reversed extension method")

Is it possible in C# to get a reference to a member function without specifying the object, so that it is usable like a static extension method, taking the object as first...
Jon Skeet
people
quotationmark

If you mean "can you create a delegate like that" then the answer is "yes, but it's slightly ugly". I don't think you can use a method group conversion, but you can use reflection and Delegate.CreateDelegate, e.g. MethodInfo method =... more 9/7/2016 7:30:37 AM

people

Initializing java.math.BigInteger

Sorry cause this might look like a stupid yes or no question but I'm very new to this so I need an answer. BigInteger i = BigInteger.valueOf(0); and BigInteger i = new...
Jon Skeet
people
quotationmark

They both end up with a reference to a BigInteger with a value of 0, but they're not identical in effect. In particular, as valueOf is a static method, it can make use of caching, and return the same reference if you call it... more 9/7/2016 6:21:32 AM

people