Browsing 7239 questions and answers with Jon Skeet

Stop other classes from adding to lists with "private set;"

Currently, I have a class (classB) that has a list --v class classB { public List<int> alist { get; private set; } ... And I can accessing this class from another...
Jon Skeet
people
quotationmark

Well, there are various options here. You could change the property type to one which only provides a read-only interface (e.g. IReadOnlyList<T> or IEnumerable<T>. That doesn't stop the caller from casting the returned... more 7/18/2015 10:01:44 AM

people

Will an unreferenced object used in an anonymous class instance not expire?

Assume this code: public class Foo { public static Thread thread; public String thing = "Thing!!"; public static void main(String[] args) { new...
Jon Skeet
people
quotationmark

The anonymous inner class will have a reference to the Foo, as that's how it is about to access thing. It's as if you had: public class FooRunnable implements Runnable { private final Foo foo; public FooRunnable(Foo foo) { ... more 7/17/2015 7:16:08 PM

people

Assigning a lambda as a parameter to a generic method called through reflection

Consider a generic method as follow: class SomeClass { public static void SomeMethod<T>(Func<T>); } I would like to call this method using reflection. This is...
Jon Skeet
people
quotationmark

It doesn't sound like you really need a lambda expression here. You can build a function to return a constant value very easily using expression trees: var expression = Expression.Constant(obj, type); var delegateType =... more 7/17/2015 5:22:57 PM

people

Set auto property's value via reflection

I've seen various threads on how to call a property's private setter via reflection. However, what about auto-properties without a setter? public class Test { public string...
Jon Skeet
people
quotationmark

Is it possible to set the value of this readonly property using reflection? No. Those properties are backed by read-only fields. There is no setter; any assignments performed in the constructor write directly to the fields. If your... more 7/17/2015 5:00:09 PM

people

C# MemoryStream to text file not Completing

I'm having an issue with using a memory stream on a large text file function in C#. The output text file doesn't contain all the information, it's missing the last few lines of...
Jon Skeet
people
quotationmark

You're flushing the MemoryStream, but not the StreamWriter. It's possible that that's buffering. Call sb.Flush() instead of stream.Flush(). (Flushing a MemoryStream doesn't do anything anyway.) Also note that you can use... more 7/17/2015 4:39:52 PM

people

What is the best way to store NodaTime based values in PostgreSQL?

We are implementing PostgreSQL and have also decided to use NodaTime instead of standard .NET DateTime. PostgreSQL has a "timestamp with time zone" type, and NodaTime returns a...
Jon Skeet
people
quotationmark

From what I understand, "timestamp with time zone" is a misnomer anyway - it's just "you supply a time zone with a local value, PostgreSQL will store UTC"... in other words, it's really an extra conversion rather than storing more... more 7/17/2015 2:10:09 PM

people

Timestamp in ISO 8601 the last 6 digits yyyy MM dd'T'HH:mm:ss.?

I have timestamps looking like this: 2015-03-21T11:08:14.859831 2015-03-21T11:07:22.956087 I read a Wiki article on ISO 8601, but did not get the meaning of the last 6...
Jon Skeet
people
quotationmark

Is it just more precise than milliseconds? Yes, it's microseconds in this case. ISO-8601 doesn't actually specify a maximum precision. It states: If necessary for a particular application a decimal fraction of hour, minute or... more 7/17/2015 1:51:52 PM

people

Does the JAR entry point actually have to be inside the JAR?

A bit of context I am trying to design a minimal framework with a launcher which will be common to several projects. I am however encountering difficulties when it comes to...
Jon Skeet
people
quotationmark

Does the JAR entry point actually have to be inside the JAR? No, it doesn't. It's entirely reasonable to put the launcher into a separate jar file, and so long as that's in the classpath specified by the manifest, it should be fine.... more 7/17/2015 10:02:17 AM

people

How to rise PropertyChanged event without passing property name as a string?

In wpf we often use following pattern for bindable properties: private Foo _bar = new Foo(); public Foo Bar { get { return _bar; } set { _bar = value; ...
Jon Skeet
people
quotationmark

Use C# 6 and the nameof feature: OnPropertyChange(nameof(IsBarNull)); That generates equivalent code to: OnPropertyChange("IsBarNull"); ... but without the fragility. If you're stuck on earlier versions of C#, you can use expression... more 7/17/2015 9:16:05 AM

people

When is "Constant" class variable initialized

I have a constant int variable defined as a class variable: My class is defined like this: public class ABC : XYZ { private const int constantNumber = 600; public...
Jon Skeet
people
quotationmark

It's available even without the class being initialized! Basically, everywhere the constant is used, the compiler will inline the value. For example: public class Constants { public const int Foo = 10; static Constants() { ... more 7/17/2015 8:44:21 AM

people