Browsing 7239 questions and answers with Jon Skeet

Invoking Events, h(args) vs EventName?.Invoke()

I've always invoked events as so void onSomeEvent(string someArg) { var h = this.EventName; if (h != null) { h(this, new MyEventArgs(someArg)); } } Today VS...
Jon Skeet
people
quotationmark

Presumably the ? after the event name is a check if the handler is null? Yes. It's the null conditional operator, introduced in C# 6. It's useful in all kinds of ways. I've used the first example for years and realize it prevents... more 5/6/2016 3:46:29 PM

people

Is constructor returning object never used wasteful?

I'm working with a code base that instantiates objects without assigning to a variable, like so: new MyNonStaticClass(); Inside the constructor are calls to static...
Jon Skeet
people
quotationmark

Is there a performance benefit to changing these to static classes? Yes - you won't be constructing a load of pointless objects which the GC then has to collect. Is that significant in your application? Hard to tell. More... more 5/6/2016 9:48:00 AM

people

Object.ReferenceEquals prints true for two different objects

How is the below code is printing true? string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x,y)); I expected this...
Jon Skeet
people
quotationmark

This is an undocumented (as far as I'm aware) optimization in the CLR. It's very odd, but yes: the new operator is returning the same reference from two calls. It appears to be implemented in CoreCLR as well on Linux (and even on... more 5/6/2016 9:33:06 AM

people

Illegal characters in path in console application

I am going to access a text file from console application and having some data like this- Auto 2017 Mech 2056 CSE 2016 Error occurred when read file, please see the attached...
Jon Skeet
people
quotationmark

You're trying to take the text of the file, and then load it as if that were another filename. In other words, you're asking to read a file called "Auto 2017 Mech 2056 CSE 2016" including line breaks. That file doesn't exist, does it? Get... more 5/6/2016 5:53:12 AM

people

Error in expression tree: System.InvalidOperationException: variable 'message' of type 'A' referenced from scope '', but it is not defined

I need to build Action which will represent this code: (new P()).Handle(argument type of A) I have an expression for...
Jon Skeet
people
quotationmark

The problem is that you're calling Expression.Parameter twice, so you've got two different parameter expressions. They don't bind by name, unfortunately. So the solution is simply to use multiple statements, creating a ParameterExpression... more 5/5/2016 3:27:45 PM

people

Convert error between Expression and Delegate

We can write below code: Func<string, string> func = x => x + x; We also can write: Expression<Func<string, string>> exp = x => x + x; But when I...
Jon Skeet
people
quotationmark

So what's the real type of x => x + x; It doesn't have one. A lambda expression is implicitly convertible into compatible delegate types and expression tree types (with some restrictions) but that decision is made at compile-time,... more 5/5/2016 10:33:44 AM

people

adding single bit in MemoryStream using BinaryWriter

Currently I am working to develop a MemoryStream using BinaryWriter. I have several types (i.e. datatype) of data which I am putting in MemoryStream one by one. and for sake of...
Jon Skeet
people
quotationmark

so my question how can I write and read (using BinaryReader) single bit to/from MemoryStream ? You can't. The smallest "unit" of data in a stream is a byte. If you've got some other byte of which you're only using 7 bits, you could... more 5/5/2016 5:46:07 AM

people

Linq Expressions > Create CallExpression with Equals using enum types

I'm trying to build an CallExpression like: f.Equals(s); where, f and t are enum SType. So, var equalsMethod = typeof(SType).GetMethod("Equals", new[] { typeof(SType)...
Jon Skeet
people
quotationmark

When you call f.Equals(s) you're really doing: f.Equals((object)s) ... because Enum and ValueType don't overload Equals. So basically you need a conversion in there - and you can be clearer about the Equals method you're calling,... more 5/4/2016 10:50:37 AM

people

Request.Params is replacing character "+" for " " (whitespace) .net

I have the follow iframe element <iframe id="iFrameMain_01_01_01_01" frameborder="0" width="100%" height="100%" scrolling="no" style="OVERFLOW:hidden;" ...
Jon Skeet
people
quotationmark

The character "+" is replacing by white space. Yes, it would be - because that's what URL decoding is meant to do for query parameters. When you URL-encode a space in a query parameter, you get a + in the resulting URL, so... more 5/3/2016 12:52:20 PM

people

Does this statement calls constructor obj=new checker[10]?

class checker { public checker() { System.out.println("In the constructor"); } } public class StringTesting { static String string1; static String...
Jon Skeet
people
quotationmark

Your current code doesn't create any objects of type checker - it just creates an array which is capable of holding references to objects of type checker. Initially every element in the array has a value of null. It's important to... more 5/2/2016 7:13:52 AM

people