Browsing 15711 questions and answers with Jon Skeet

Incomprehensible record of a variable

Hello im learning C# i got my variable assignment like this: i = i < 0 ? i + 3 : i > 2 ? i - 3 : i; Can someone please explain to me, what does that mean?
Jon Skeet
people
quotationmark

The ?: operator is the conditional operator. In an expression of the form x ? y : z First x is evaluated If the result of x is true, then y is evaluated and becomes the result of the overall expression Otherwise z is evaluated and... more 6/14/2017 6:40:25 AM

people

OffsetTime in NodaTime

I'm looking for some kind of OffsetTime support in NodaTime, but am not seeing anything. I am receiving data in a format such as "17:13:00+10:00". I am to treat this as a time...
Jon Skeet
people
quotationmark

No, there isn't anything representing this in Noda Time. It's a pretty odd kind of value, as in at least many time zones, the offset will vary over the year. I understand that sometimes we need to work with what we've got though. I'd... more 6/13/2017 8:08:50 PM

people

Why are two hours added to my time

I'm struggling with time parsing. My input is a time string ending in "Z". I would expect that to be UTC. When I parse that string two hours are added to the result. I do not know...
Jon Skeet
people
quotationmark

By default, DateTime.Parse converts to a "kind" of Local. (Print out outTime.Kind to verify that.) So it understands that the source is universal - but it's adjusting it to system local time. Note that culture has nothing to do with time... more 6/13/2017 6:31:39 PM

people

MessageBox doesn't show all given string

I have a c# program and am trying to call a messageBox with a specific string(normal string, nothing special), And when reaching a variable to concatenate with the string, it...
Jon Skeet
people
quotationmark

The Win32 GUI libraries terminate strings if they find a \0 character (U+0000, Unicode "null") in them. For example, if you had: MessageBox.Show("First part\0Second part"); then only First part would be displayed. There are at least... more 6/12/2017 5:54:51 PM

people

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