Browsing 7239 questions and answers with Jon Skeet

C# Return Results of Dictionary Lookup or if NULL return something else

Here is my current function (it works but wondering if there is a cleaner way to do it). public static Dictionary<int, double> profileNetworkLowCostPriorityList; public...
Jon Skeet
people
quotationmark

It sounds like you just want TryGetValue, basically: public static double DetermineNetworkSortOrderValue(int network, double fee) { double dictionaryFee; return profileNetworkLowCostPriorityList == null || ... more 2/11/2016 7:01:22 PM

people

kubernetes replication controller

i've a simple kubernetes cluster with a master and 3 minions. In this scenario, if i run a simple pod of a nginx or a mysql it works properly but, if i change type of KIND into...
Jon Skeet
people
quotationmark

It looks to me like the selector for your service is wrong. It's looking for a label of name: nginx, but your pods actually have app: nginx. Try changing your service file to: apiVersion: v1 kind: Service metadata: labels: name:... more 2/11/2016 4:09:42 PM

people

C# Timer With Lambda Instead of Method Reference?

Let's say I have the following code: var secondsElapsed = 0; Timer myTimer = new Timer(); myTimer.Elapsed += new ElapsedEventHandler(...
Jon Skeet
people
quotationmark

Sure, just: myTimer.Elapsed += (sender, args) => secondsElapsed++; The parameters for the lambda expression have to match the parameters for the delegate that you're trying to convert it to, basically. Depending on whether the Timer... more 2/11/2016 2:41:53 PM

people

parsing nested json with json.net

I have problems with json deserialization , below is my json { "_id" : ObjectId("56bc28c436b252c406a67f17"), "empname": "dhiraj", "empcode": "123a", "level": { ...
Jon Skeet
people
quotationmark

There are several problems here: The code you've given won't compile, as you've specified a class called level but used it as Level You're trying to deserialize a List<Employee>, but your JSON only specifies a single Employee... more 2/11/2016 9:30:20 AM

people

Overriding GetHashCode() C# Complex way or simple way?

The more I read the more confused I am from blogs, to MSDN, to other stackoverflow questions and responses. This article:...
Jon Skeet
people
quotationmark

To my mind, you should always override GetHashCode if you override Equals - otherwise you're violating the contracts of those methods. However, for mutable types, client code needs to be aware that if it does mutate a value used as a key... more 2/10/2016 7:48:41 PM

people

Join on Condition With Linq Methods

Say I have a class Class Record { int Id int StartDate } Class DBRecord { int Id DateTime StartDate DateTime EndDate } How could I join these using the linq...
Jon Skeet
people
quotationmark

The Join method is for an equijoin - not an arbitrary condition. You might want to just use SelectMany to get a "full join" and then Where: this.Records() .SelectMany(_ => context.DBRecords, (x, y) => new { x, y }) .Where(z... more 2/10/2016 5:58:33 PM

people

Will a C# Class Get Instantiated Twice When Referenced from A Different Executable?

I have a class SystemConfig on its own project and executable. I have implemented a singleton for that class with a property called Instance. So my question is, when I...
Jon Skeet
people
quotationmark

Not only will it not be the same if you're running a different executable - it will not be the same if you run the same executable twice as two separate processes. Basically, your singleton is likely to only be a singleton for a single... more 2/10/2016 3:38:18 PM

people

Convert specific country date time to UTC with NodaTime

I would like to convert specific date time value to UTC with NodaTime by giving country code. For example country is Turkey, country code is TR and specific date time is "Feb 5,...
Jon Skeet
people
quotationmark

Well, you can't do it just with a country code - you need a time zone. In some countries there are multiple time zones. Once you have a time zone as a DateTimeZone (either via a BCL TimeZoneInfo or from the TZDB time zone provider) you... more 2/10/2016 1:30:06 PM

people

How to change property/Attribute from json string in c#?

I am having json string like {"name":"studentname","Lastname":"lastnameofstudent"} i want to change name property/key to FirstName not a value of this property. using Newtonsoft...
Jon Skeet
people
quotationmark

The simplest way is probably just to assign a new property value, then call Remove for the old one: using System; using Newtonsoft.Json.Linq; class Test { static void Main() { string json = "{ 'name': 'SUSHIL' }"; ... more 2/10/2016 10:15:11 AM

people

How to read /write XORed txt file UTF8 in java?

what i did so far : I read a file1 with text, XORed the bytes with a key and wrote it back to another file2. My problem: I read for example 'H' from file1 , the byte value is...
Jon Skeet
people
quotationmark

You're not reading the file as bytes - you're reading it as characters. The encrypted data isn't valid UTF-8-encoded text, so you shouldn't try to read it as such. Likewise, you shouldn't be writing arbitrary byte arrays as if they're... more 2/10/2016 9:43:59 AM

people