Browsing 7239 questions and answers with Jon Skeet

How to use bitwise NOT operator with shift operator in C#?

I don't understand how this expression works. ~(1 << 1) = -3 What I do understand is that 1 << 1 has a value of 10 in binary and 2 in base 10. How did it get a -3...
Jon Skeet
people
quotationmark

The bitwise inverse operator is entirely separate from the shift here. You've started with input of 10 (binary) - which has a full 32-bit representation of 00000000_00000000_00000000_00000010 The bitwise inverse is... more 6/12/2017 1:56:40 PM

people

How is a c# lambda capturing variables

Why does the following code print 11 twice? int i = 10; Action fn1 = () => Console.WriteLine(i); i = 11; Action fn2 = () =>...
Jon Skeet
people
quotationmark

If that is the case shouldn't my example have printed 10 & 11? No, because you've only got a single variable - fn1 captures the variable, not its current value. So a method like this: static void Foo() { int i = 10; ... more 6/12/2017 9:44:09 AM

people

Confusion about Object.GetType().Name

using System; namespace somens { class GettingTypeName { static void MethodUsingVar() { var someInt = 0; ...
Jon Skeet
people
quotationmark

The "member" here isn't your variable - it's a member of the namespace (or assembly, or module if you want). (It's unfortunate that the documentation is inherited from MemberInfo - if it said "the name of the type" it would be... more 6/10/2017 11:18:24 AM

people

Determining which overrides method is actually invoked

When a subclass object is casted to its superclass, for example Superclass obj = new Subclass(); and the classes were defined public class Superclass{ public void...
Jon Skeet
people
quotationmark

Basically, why is the version of thisMethod() defined in the subclass being invoked through the Superclass object obj? Because that's basically the point of inheritance - it allows an implementation to override, without the calling... more 6/10/2017 10:38:17 AM

people

Change value of each element by linq

Is it possible to do sth like this in LINQ: int[] d = new int[c.Length + 1]; int e = 1; d.ToList().ForEach(r => { r =...
Jon Skeet
people
quotationmark

Yes, it would, for two reasons: You're creating a copy of the original array as a List<int>, and then trying to modify the List<int>. That wouldn't modify the array. Your lambda expression changes the value of the parameter,... more 6/10/2017 8:54:02 AM

people

Cant print elements of arraylist or list

When i call ReadList function it dosent print the elements,what i am doing wrong?i tried even with normal list.What i want to do is if i create 2 accounts by CreateAccount...
Jon Skeet
people
quotationmark

Your CreateAccount method does this: Creates a new list Populates the list Does nothing with the list Your ReadList method does this: Creates a new (empty) list Prints the contents of the list The list created in CreateAccount() is... more 6/9/2017 7:57:43 PM

people

How to parse xs:date with NodaTime?

I have to process XML documents sent by various external systems that also contain some dates. xs:date allows adding a "time zone", i.e. an offset, to a date. E.g....
Jon Skeet
people
quotationmark

I would parse the value as OffsetDateTimePattern as that most closely represents the information you actually have in the text. My experience is that it's best to parse in a form that retains all the information you need, then use the Noda... more 6/9/2017 5:54:47 AM

people

C# executing code using condition ? method call : method call

I'm trying to add to one list or another based on a condition using ? : syntax, is this possible in c#, the syntax I was using does not compile List<int> Negatives = new...
Jon Skeet
people
quotationmark

The conditional operator evaluates one expression or other to compute a value. It doesn't execute void-returning methods. You can use the conditional operator here though, to decide which list to add to: (number >= 0 ? positives :... more 6/8/2017 9:44:20 PM

people

C# notation understanding Select(int.Parse)

I found a little script that I understand fully. I've got a string with "1 -2 5 40" for example. It reads the input string, splits it into a temporary array. Then this array is...
Jon Skeet
people
quotationmark

int.Parse is a method group - what you're seeing is a method group conversion to a delegate. To see it without LINQ: Func<string, int> parser = int.Parse; int x = parser("10"); // x=10 It's mostly equivalent to: Func<string,... more 6/8/2017 11:40:20 AM

people

How to use single object instance for class to set the multiple member value?

Curretly i am using below code to set the DateRanges Class member variables. DateRanges DateRanges1 = new DateRanges(); DateRanges DateRanges2 = new DateRanges(); ...
Jon Skeet
people
quotationmark

To make this code cleaner, I'd suggest: Renaming Lastmonth etc to follow .NET naming conventions Not bothering with any separate variables (but if you do, make them follow .NET naming conventions too) Renaming DateRanges to DateRange (as... more 6/8/2017 10:15:33 AM

people