Tag Info

Hot answers tagged

28

In principle there's no difference between inheriting from generic types and inheriting from anything else. However, why on earth would you derive from any class and not add anything further?


18

ToString usage No, you shouldn't use ToString here. String concatenation automatically transforms non-strings into strings, which means that your two variants are nearly┬╣ identical: When one or both operands are of type string, the predefined addition operators concatenate the string representation of the operands. Source: C# Language Specification: ...


13

Instead, you should use string formatter. It's easier to format your number into string representation or localization. e.g.: string expression = string.Format("Expression: {0} + {1} = {2}", a, b, sum); More info on MSDN. However, string formatter is less readable (and maybe also performance) than string concatenation.


10

I don't know C# very well, but I believe this is the only way to implement a typedef, which C++ programmers commonly use for a few reasons: It keeps your code from looking like a sea of angle brackets. It lets you swap out different underlying containers if your needs change later, and makes it clear you reserve the right to do so. It lets you give a type ...


8

what if; what if; what if? YAGNI Seriously. If someone wants to use different implementations of File or Math or Console then they can go through the pain of abstracting that away. Have you seen/used Java?!? The Java standard libraries are a fine example of what happens when you abstract things for the sake of abstraction. Do I really need to go ...


8

There is another reason why you may want to inherit from a generic type. Microsoft recommend avoiding nesting generic types in method signatures. It is a good idea then, to create business-domain named types for some of your generics. Instead of having a IEnumerable<IDictionary<string, MyClass>>, create a type MyDictionary : ...


8

I can think of at least one practical reason. Declaring non-generic types that inherit from generic types (even without adding anything), allows those types to be referenced from places where they would otherwise be unavailable, or available in a more convoluted way than they should be. One such case is when adding settings through the Settings editor in ...


7

In a word Simplicity. When you decouple too much, you get the hello world from hell. void main(String[] args) { TextOutputFactory outputFactory = new TextOutputFactory(); OutputStream stream = outputFactory.CreateStdOutputStream(); Encoding encoding = new EncodingFactory.CreateUtf8Encoding(); stream.Encoding = encoding; SystemConstant ...


6

Using static classes like this is simple and efficient. Like most things that are simple and efficient it comes with drawbacks: your code will be difficult to unit test because any object that uses these static methods cannot easily be separated from them, so tests will have to include them, whereas usually we'd aim to use either stub or mock ...


6

I think you need to look into some history. C# has been used for a lot longer than it has had generic types for. I expect that the given software had its own StringList at some point in history. This may well have been implemented by wrapping an ArrayList. Then someone a to refactoring to move the code base forward. But did not wish to touch 1001 different ...


6

I wouldn't say it's an abuse. It's certainly something you can do with generics, and it sometimes makes your code cleaner. Here's the caveats: While it's super easy to go from generic to Type it's very difficult to do the opposite. If your caller is likely to have a Type variable rather than calling your stuff with an explicit type... maybe reconsider. It ...


3

Of the now partially outdated but still useful article from MSDN: "When to Use Delegates Instead of Interfaces (C# Programming Guide)", a few of the rules-of-thumb stand out: Use a delegate in the following circumstances: (3) The caller has no need to access other properties, methods, or interfaces on the object implementing the method. My ...


2

I definitely must have the IByteStream and the IFloatStream interface. Without a compelling reason, I definitely say you don't. Do you make a ByteList or a FloatList? No, you leave List be and let people parameterize it. This sort of aliasing can occasionally be useful, but more often it is a code smell that your interface/class is too abstract (or ...


2

Does laziness within an application naturally yield a greater chance of race conditions? Of course it does. Properly written multithreaded code will not have race conditions, of course; race conditions are caused by not thinking about all the possible outcomes of ordering between multiple threads. This is not to say, however, that lazy loading directly ...


2

Let me ask a question : what you happen if in your PHP version I would write: // PHP class Sample { public function __construct() { $dispatcher = new EventDispatcher(); $dispatcher->addListener("sample.event", array($this, "onEvent")); $dispatcher->dispatch ("sample.event", new WarnEvent()); // oops? } ...


2

Because in your first question you professed a lack of knowledge about dependency injection, I will explain it in simple terms: public class ThingThatNeedsABuilding { private Building building; public ThingThatNeedsABuilding(Building building) { this.building = building; } } public class OtherThingThatNeedsABuilding { private Building ...


1

This screams out for a Log4Net custom appender. You could write your own, have it check the active item to determine whether to log or not, and log to a per-item memory buffer.


1

I am not familiar with C#, but it is my conviction that one should try to limit global values and classes as programming in general. Of course depending on the sircumstances. Always try and keep your classes to a minmum (but logical) responsibility. You should do some reading on the subject "low coupling high cohesion". If you have a lot of classes ...


1

In some cases it is impossible to use generic types, for example you can't reference a generic type in XAML. In these cases you can create a non-generic type which inherits from the generic type you originally wanted to use. If possible, I would avoid it. Unlike a typedef, you create a new type. If you use the StringList as an input parameter to a method, ...


1

One of the most important things when using a programming language is understanding what it's weaknesses are. As many languages have substantially different weaknesses, it is often a very bad idea to directly port code from one to another. In both examples you have provided, you are presented with a fairly severe typing issue. Even the dictionary has a ...


1

The Matrix.MeshGrid method takes inputs as type of T[] (not T[,]). Method signature: public static Tuple<T[,], T[,]> MeshGrid<T>(this T[] sequence1, T[] sequence2); Example: var xi = new double[] {1, 2, 3, 4, 5}; var yi = new double[] {6, 7, 8, 9, 10}; Tuple<double[,], double[,]> grid = Matrix.MeshGrid(xi, yi);



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