Tag Info

Hot answers tagged

8

C# requires you to explicitly declare a variable, in addition to assigning it a value. This is done so that its clearer to both the compiler and the person reading the code that we're defining a new variable here, not using a previously-defined one, which can simplify parsing and make the code much clearer. As you said, the statement ExampleClass c = new ...


6

Let's assume features start with 0 points, rather than with -100 points (due to costs associated with implementation, design, etc.). This feature strikes me as confusing. I've already declared that a is an A. If I want to know the compile-time type of a, I only have to look in one place, at the declaration. While I acknowledge that the implementation is ...


6

It is entirely possible although you have to be consistent with your types. The technical term for this is closure. public Action MakeAction(State s) { var me = new Item(); return () => s.Stack.Push(me); }


6

It's less verbose, increases flexibility, and decreases coupling. What's not to like? Let's compare the use of a closure for handling an event versus using a class. The class must implement a specific interface. This means the event handler must not only have specific arguments, but also a specific name. If you want that same object to be able to handle ...


4

For new code there isn't any particular value -- by and large they are around for backwards compatibility concerns -- NameValueCollection is deeply embedded in significant parts of ASP.NET for example. Many venerable libraries are using the non-generic and specialized collections internally. Some of the specialized collections don't have a direct generic ...


4

Maybe I don't understand closures fully but take for example C#, why would I use closures when I can use classes? Are you really going to write a whole new class for every single .Where clause in your code? I mean you can do that, but using closures allows you to concisely write anonymous functions in the spot they're used.


3

The .NET heap is an object graph, only boxed types will have a contiguous memory block for you to scrape. If you pinned an object and everything it referenced, and everything that stuff referenced recursively pinning the entire object graph you wanted to serialize, then you could start walking the object graph scraping it all into a contiguous byte array ...


3

It's not that it's a bad idea, it's just that there's no point in elevating this to the point of language feature, not even syntactic sugar, you can very easily implement this yourself in C#: public static class ExtensionMethods { public static void AsIf<T>(this object target, Action<T> todo) { if (target is T) todo((T)target); ...


3

Yes, it bothers other people. It's a major motivation for people choosing dynamically typed languages. What you're talking about in the context of a statically typed language is called type inference, which basically means the compiler can tell what type a variable is by what type it is initialized to. It has been a full feature in languages like Scala ...


3

Your code is your own. Generated code is also your code. While Microsoft may have built the mechanism to output it, it is no more owned by them than a brochure your company prints is owned by HP. Using an IDE to write your code does not make the IDE developers own it. Note, however, that your code is, more accurately, your employer's if you create it on ...


3

There's a third option: mock the object with the event and use behavior testing to verify the mock's subscriptions at given points. Several mocking frameworks allow for this. public interface INeedToBeMocked { public event EventHandler EventRaised; } NSubstitute var mockedItem = Substitute.For<INeedToBeMocked>(); ...


3

As david.pfx writes in his comment, there is a bit of info missing, but I am willing to give it a shot. I am making an assumption in my answer. The reason that C and D both want to use B is, at least in part, to use M100Communicator Thereafter it kinda depends on if other modules are around that want to use the ICrt570 interface. Scenario A If it is ...


3

Dagger for Java/Android does that. It sacrifices some runtime magic (like Guice's) to offer an almost completely compile-time codegen experience, including converting most runtime errors to compilation errors. Would be cool in .NET too.


3

Here is the general approach: Read a dictionary file and organize all words in a trie data structure. Many Unix systems have such files in the /usr/share/dict/ directory. Find possible matches of a prefix of your input in the trie. This will usually produce multiple matches, for example theologyisabout begins with theology and the. If we remove the matched ...


2

First, you use Normal distribution to generate population of each village. This should give you number that is pretty close to total population. To get exact population, just add or remove the difference evenly across all villages. The problem of this algorithm is that there is some probability of generating negative population. But that heavily depends on ...


2

Is there a better way to solve this problem? Yes! C# allows you to override the += and -= syntax. I'm not sure how easy it is to supply in common mocking frameworks like Moq, but it should be trivial to build your own fake object that has hooks into the subscribe and unsubscribe methods of your interface: private Action foo = () => {}; ...


2

No, the repetitious syntax shouldn't bother anyone. It's kind of like complaining about the the three statements within a for loop. for(int x = 0; x <= 100; x++) { /* do work */ } Sure, you could use syntactic sugar for mocking this common pattern as below, but you don't gain a lot by any sensible alternative and, like any usage of sugar, you lose ...


2

Under normal circumstances, cyclical references are not a problem for the GC, because it can detect them. But here, the whole object graph looks like this (based on looking at the reference source): So, the problem isn't just the cycle, it's that you have a static field that references the cycle. What you could do to fix this is to break the cycle ...


1

I've seen an AsIf extension method implemented as such: private static void AsIf<TBase, TChild>(this TBase source, Action<TChild> action) where TChild : TBase { if ((source is TChild) && (action != null)) { action((TChild)source); } } The generic constraints make sure you're attempting to "as-if" something ...


1

I like the idea. It's also not dangerous if you take into consideration some cases. Consider if (a is B) { a.BMethod(); // OK a = new B(); // OK a.BMethod(); // OK a = null; // Disallow? (Probably OK) a.BMethod(); // Disallow? (Probably OK) a = new A(); // Disallow? (Probably bad) a.BMethod(); // Disallow? } It certainly is ...


1

Consider a class that implements IDisposable, but when it is not disposed by its user, it will not become eligible for garbage collection, thus its destructor won’t run, and resources will be leaked. Your presumption is incorrect. All classes are disposed and a properly written implementation of IDisposable will not leak resources. To be absolutely ...


1

The "Correct" approach to modify an XML file is any method that results in well-formed and valid XML, with the least bit of work on your end and an appropriate amount of repeatability in the future. If you already have a object reference that typically manages and creates them, deserialization and re-serialization may meet that requirement. However, ...


1

Is there any known good architecture that separate the layers by modules on the server side? Modules and layers are two different concepts. Layers are typically used to describe the way code is deployed in a environment. However, Modules is about organizing code according to the functionality. There are other perspectives to modules where it could be ...



Only top voted, non community-wiki answers of a minimum length are eligible