Browsing 7239 questions and answers with Jon Skeet

Pattern for Concurrent up counter with c# and TPL Dataflow

Given the following code, how does the count++ behave under concurrent access in c#: int count = 0;int Count=0; ActionBlock<Tuple<CloudBlockBlob,...
Jon Skeet
people
quotationmark

Yes, that's entirely possible - at the very least theoretically - but no, you don't need a lock. Just use Interlocked.Increment: var progressDone = Interlocked.Increment(ref count) / (double) Count; (It's possible that depending on your... more 4/8/2014 4:01:38 PM

people

Accessing protected members in a different assembly

using System.IO; using System; using Assembly2; // DLL 1 namespace Assembly1 { class class1 : class2 { static void Main() { Console.WriteLine(new...
Jon Skeet
people
quotationmark

You can access the protected member from a different assembly, but only within a subclass (as normal for protected access): // In DLL 1 public class Class3 : class2 { public void ShowSample() { Console.WriteLine(sample); ... more 4/8/2014 2:04:19 PM

people

Find interfaces defined in the class

How to find all available interfaces defined in the class using Apache BeanUtils, MethodUtils etc. ? public class MyClass() { ..... public interface Interface1{}; public...
Jon Skeet
people
quotationmark

I suspect you just want Class.getClasses(): Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object. This includes public class and... more 4/8/2014 1:43:28 PM

people

String to Date parsing

I am getting a string and i want to parse that string as date and want to store it in DataTable. string can be in formats 1- "2014/23/10" 2- "2014-23-10" { string...
Jon Skeet
people
quotationmark

DateTime.ParseExact or DateTime.TryParseExact are appropriate here - both will accept multiple format strings, which is what you need in this case. Make sure you specify the invariant culture so that no culture-specific settings (such as... more 4/8/2014 12:53:12 PM

people

Smartly Decoding Hex Input (e.g 0x32, 0x3, 32 etc...)

I seem to be going around in circles with this one... There are many methods to achieve this and I could use a few if statements like I've done int he example below, but I want a...
Jon Skeet
people
quotationmark

Why not just remove 0x if the string starts with it? There's no need to use a regular expression for this - just a combination of startsWith and substring is simpler to understand (IMO): String hexString = response.getResult(); if... more 4/8/2014 12:48:23 PM

people

How Create programmaticaly Expression.Lambda<Func<TEntity, TProperty>> with variable Type?

I want to create below code var lambda = Expression.Lambda<Func<TEntity, TProperty>>(expName, entity); but TProperty type is variable and change in loop and i can...
Jon Skeet
people
quotationmark

You can use Expression.Lamdba(Type, Expression, params ParameterExpression[]) - you'd use typeof(Func<,>).MakeGenericType(typeof(TEntity), nameType) to create the relevant type. That will just give you a LambdaExpression though.... more 4/8/2014 10:44:42 AM

people

How good a practice is this?

Say I have a Java application which handles thousands of users. So for each user action a certain block of code will be executed repeatedly and I am creating millions of temporary...
Jon Skeet
people
quotationmark

You should absolutely let Java handle this on its own. All you're doing is making your code harder to read and maintain. All the variables you're nulling out are going out of scope at the end of the method anyway - and the GC knows... more 4/8/2014 10:14:04 AM

people

Best Practice for Writing Data Using protobuf

We need our protobuf messages to contain as little data as possible. So what are the best practices we can follow in order to gain the maximum out of it. As an example writing...
Jon Skeet
people
quotationmark

As an example writing byte[] as a Srting or ByteString ? If you want to write binary data, use a bytes fields (so ByteString). A string field is UTF-8-encoded text, so can't be used for all possible byte sequences. And adding a... more 4/8/2014 8:14:47 AM

people

Method overloading with variable argument

I think this would be a stupid question a little bit, but I can not sure why it is. code; public class OverloadingTest { public static int sum(int ...a){ int sum=0; ...
Jon Skeet
people
quotationmark

The answer is in JLS section 15.12.2. Basically, the compiler tries to find any applicable method without having to expand varargs, only using varargs if it has to: The remainder of the process is split into three phases, to ensure... more 4/8/2014 7:51:41 AM

people

Error while file download

in my servlet, I am passing a file path using cookies and downloading a file as below protected void doGet(HttpServletRequest request, HttpServletResponse response) throws...
Jon Skeet
people
quotationmark

You're writing the data to the output stream, but you're not setting a content type or a filename. You should use: response.setHeader("Content-Disposition", "attachment; filename=Result.xslx"); response.setContentType( ... more 4/8/2014 6:12:32 AM

people