Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Are partial classes any different from regular classes after compilation? Are there performance differences? Are there security differences? Is the generated CIL different? etc...
I couldn't find any information about this, all of the search results are spammed with when and how to use partial classes.
No, they are same according to this. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#partial-types
A type declaration can be split across multiple partial type declarations. The type declaration is constructed from its parts by following the rules in this section, whereupon it is treated as a single declaration during the remainder of the compile-time and run-time processing of the program.
No, those are exactly same. The compiler is just joining them files together at the build time.
The perfect use case for partial classes is extending of the auto-generated code. As an example, try to imagine generated DataSet class, that has been generated from .xsd file.
If you wish to extend it, you can use partial class.
Doing so won't remove your custom code when the DataSet is regenerated with Custom Tool.
I personally believe that splitting classes into partial classes only to reduce the sizes of files is wrong, and it's reducing readability of the code.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
If I have a number of pages that all have 3 common fields, is it recommended to have a base view model class that contains these 3 fields, and then on the viewmodels for each page, inherit from this base class?
I read somewhere that this was bad practice, but I’m not sure why. As it’s removing duplication and the validation still works etc.
Thanks
Jenny
I would suggest using composition rather than inheritance. Put these 3 fields in a separate class and have a property of this class in each view model that needs them.
Composition tend to be a bit more flexible than inheritance. A typical example would be that you can only inherit from one class, but you can have as many properties as you would like.
In my opinion, this is bad practice as your project gets more complex you will need to make changes to your fields and one template you use as base class might contain different amount of fields than you needed initially.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've read Clean Code by R.C. Martin and I'm trying to adopt his suggestions about clean code as broadly as possible.
But I'm not sure how to name related classes.
Let's say I have a class named TreeDirectoryList.
I want to cut implementation of this class into many smaller classes.
Let's say I'll create a class named ParentIndexStack.
ParentIndexStack will implement functionality very dependent on TreeDirectoryList, so it's very not probable that this implementation of ParentIndexStack will be useful with any other class in the future.
But the name of ParentIndexStack is very generic, it's possible, that I'll need another class with the same name, within the same project.
So I thought I'll name ParentIndexStack more precise, like TDLParentIndexStack (prefix TDL is from TreeDirectoryList).
Would it be correct ?
I'll end with many classes starting with TDLxxxxx.
One option is to put that set of classes in their own namespace. Then you can have simple, concise names that still communicate the full meaning of the class through the namespace context.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have some constant strings that are needed in multiple classes. How can I store and retrieve these constants in a way that meets these required criteria:
No duplication of information.
No function call to retrieve constants.
No namespace symbol prior to constant (IE: Constant not Namespace.Constant)
None of the solutions I've found so far meet every criteria:
Duplicate them in every class they are needed in (violates criteria #1).
Store them in a separate static class that is referenced wherever needed (a bit like a C-style header file, violates criteria #3).
Put them in one of the classes where they are used and reference that class when they are needed elsewhere (this is seems like a bad idea because it could create circular references, and it violates criteria #3).
Put them in app.config and retrieve them whenever they are needed (This seems to break style conventions since non-configuration data is stored in a config file, and a change to the config file could break application logic. It also violates criteria #2).
Is there a solution in C# that meets every criteria?
I use option #2 often, because #1 is rife with risk of different values, and #3 breaks normally-sought class encapsulation. If you must have globals. have one Single Source of Truth for them.
You can make the code somewhat cleaner with a using alias:
using static namespace.StaticClassName;
VB.Net has Module Statement that can be used for global variables that can be accessed without using the name of the module, but C# doesn't have anything like that.
In C# I use #2, but with const. That way the value is shown when you hover it in Visual Studio:
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are generics in C#, illustrated with a simple example? What are some related articles or websites for this topic?
Generics refers to the technique of writing the code for a class without specifying the data type(s) that the class works on.
You specify the data type when you declare an instance of a generic class. This allows a generic class to be specialized for many different data types while only having to write the class once.
A great example are the many collection classes in .NET. Each collection class has it's own implementation of how the collection is created and managed. But they use generics to allow their class to work with collections of any type.
http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx
There is really nothing special about Generics in C#. C# just likes to take well-known concepts and call them something different (e.g. calling procedures "static methods" or calling flatMap "SelectMany"). In this particular case, Generics are just C#'s name for rank-1 parametric polymorphism.
From MSDN:
Generics are the most powerful feature of C# . Generics allow you to
define type-safe data structures, without committing to actual data
types. This results in a significant performance boost and higher
quality code, because you get to reuse data processing algorithms
without duplicating type-specific code. In concept, generics are
similar to C++ templates, but are drastically different in
implementation and capabilities.
https://msdn.microsoft.com/en-us/library/ms379564.aspx
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am interested in best design pattern on below issue. Say I have 2 classes with different number of properties. I need to map instance of source class to target class' instance. Mapping source property may not be simply as just equal. There may conditional check and etc. As simplest way would be writing method and accept source class'object as parameter. Then manipulate properties and initialize target class' object. However it is not good, as there will be duplicate code and logic. Because there are will be many type of source classes. So, I will be forced writing code fach convertion. Something comes to my mimd generic methods? Thanks for your time.
Automapper worked fine for our team.