As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Unfortunately, C# does not have anything like C++'s friend. Hence, for instance, in order to solve something like the classic matrix/vector multiplication example (where efficiency can be gained by befriending matrix and vector, such that each class has access to the private members of the other), I have to define the class members internal.
Now I know myself, and improper encapsulation will lead to messy code, sooner or later. Hence I would like to keep the internal universe as small as possible.
This will lead to very small assemblies.
Does that have any drawbacks, or doesn't that matter?
Answer to question in the title: how small assembly should be?
There is no particular requirements or recommendations on size of assemblies*.
*Insane number of assemblies (probably in thousands) in theory may slow down loading due to need to lookup information in more places.
When picking size consider:
proper encapsulation
ease of editing (large number of solutions vs. several large one both have drawbacks and benifits at build/edit/deploy time)
technical restrictions (like GAC deployment, partially trusted code, anyCPU/x86/x64 requirements)
If you don't like internal cause it's too open (to the rest of your api), make those classes that need to be open to eachother a separate microassembly containing just this small set of classes. You can always embed that assembly in your main api-assembly.
That should both keep your classes open to eachother, and not let anybody else in (unless they blatantly ignore your assembly-structure, and place more stuff in your helper-assembly than its name implies it should contain. But who would do such a thing?! :)
Unless I don't understand:
Unfortunately, C# does not have anything like C++'s friend.
Friend Assemblies (C# and Visual Basic) does exist. It may not be exactly what C++ has, but it sounds like it would work.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have seen at least three distinct methods on StackOverflow for achieving this.
Using a MUTEX: Accepted answer to this SO question
Using the Microsoft.VisualBasic library's WindowsFormsApplicationBase: Second highest voted answer to this SO question
Using Process.GetProcessNames to check if your application is running: Method here was posted as an answer to this SO question
I'm sure there are more ways to do this as well.
I'm simply wondering if one of these is preferred and what the consequences might be if I pick the "wrong" one.
When in doubt, always prefer an implementation that's included in the .NET framework. You can have high expectations that such an implementation is tested by hundreds of thousands of programmers, has been carefully reviewed for security and usability and will be maintained for years to come.
The mutex approach is an easy one to get going. It however suffers from a pretty severe security problem. A denial of service attack is very simple to get going, you cannot keep the name of your mutex a secret and anybody can trivially create a mutex with the same name and prevent your program from ever starting up.
The process name approach is deeply flawed for the same reason. There is no guarantee that a process name is unique. Not just easy to exploit but easily triggered by accident.
WindowsFormsApplicationBase has an image problem in the eyes of C# programmers. They choke at the namespace name and assume that their program will somehow be infected with vb-isms. That's nonsense, it is just a plain .NET class that's useable in any language.
Why nobody mentioned ticking this checkbox?
It's really a matter of taste, but I favor the Mutex approach, simply due to it not requiring a dependency on the VisualBasic libaries, and using Process.GetProcessNames is a non-ideal solution (as mentioned, process names aren't always going to map to what you think they might)
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've heard various programmers suggest not including the word "private" in declarations, method signatures, etc. as private is the default scope when not specified. It can make for cleaner code but I'm interested in what the opinions are on whether you use the "private" scope on your variables, methods, etc. Tools like CodeRush that generate code for you include the word "private" so I'm curious if this is good or bad or just a matter of personal preference.
Cleaner code is more explicit as to the designer's intentions. Using private demonstrates a deliberate choice, not open to debate. Falling to the default opens up the questions: was this on purpose, or he simply forgot to include a modifier?
Remove the private and Ask your fellow developers whether they are confused or not
Personally i feel, including private make your code more readable. I would give more importance to "Readability" than "being cleaner"
In a codebase where stuff being public is an information leak (e.g. it will no longer get obfuscated), you want public to stick out. Removing private also has the same 'tide going out' effect on protected and other unnecessarily elevated visibility.
Ideally one'd use a StyleCop rule or similar to make the code actually be consistent (though that, as with all code rules should actually be agreed among the devs before someone comes to a conclusion about it).
(BTW Your contention in the premise re CodeRush's support for omitting it is incorrect - the options allow you to set method visibility etc. to be either private (OOTB) or 'default' (do not specify anything)).
It is up to compiler how to interpret methods or other class members without private, protected or public. It can be changed in nex version. So don't do it.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
We are about to implement a small automated securities trader. The trader will be build on top of the excellent quickfix FIX engine.
After due though, we narrowed our options down to implementing it in C# or in Python. Please specify the pros and cons of each language for this task, in term of:
Performance (The fact that Python uses a GIL troubles me in terms of thread concurrency)
Productivity
Scalability (We may need to scale this trader to a fully-sized platform)
EDIT
I've rephrased the question to make it less "C# vs. Python" (which I find irrelevant - both languages have their merits), but I'm simply trying to draw a comparison table before I make the decision.
I like both languages and a think both would be a good choice. The GIL might really be the most important difference. But I'm not sure if it's a problem in your case. The GIL only affects code running in pure Python. I assume that your tool depends more on I/O than on raw number crunching. If your I/O libraries handle the GIL correctly, they can execute concurrent code without problems. And even for number crunching you still have numpy.
My choice would depend on your existing knowledge. If you have experienced C# developers at hand I would go for C#. If you start absolutly from scratch and it's really 50:50, then I would go for Python. It's easier to learn, free and in many cases more productive.
And just to mention it: You might also have a look at IronPython. ;-)
For points "Performance" and "Scalability" I would suggest C# (although a large part of performance depends on your algorithms). Productivity is much of a subjective thing, but now C# has all cool features like lambda, anonymous method, classes etc which makes it much more productive.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 12 years ago.
I am trying to figure out if there is anything that you can do in c++ that you absolutely cannot do in c#?
I know that there are platforms that are targeted to native libraries, but I want to know if the lowest level c# can compare with the lowest level c++.
Device drivers. These applications operate in kernel mode, and .NET apps don't (they run in user mode). Even if you could, would you really want to? Probably not considering the overhead of the runtime and the relative difficulty of interfacing directly to hardware devices.
In software you can pretty much do anything given enough time and effort. It comes down to whether or not a certain task is practical rather than possible.
inline assembler
there are some very complex win32 signatures that cannot be used via p/invoke; the sspi security interfaces for example
Write Real Mode code. There is no CIL framework that runs in real mode, therefore C# cannot target it. C++ has been able to target real mode for decades now.
You can't use multiple inheritance in C# (Excluding interfaces).
In C++ you can overload more operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B vs: http://msdn.microsoft.com/en-us/library/8edha89s%28v=VS.100%29.aspx
Well, C# handles all the memory management, so you're limited in terms of hands on memory management. This isn't really a bad thing though, as it takes a lot of work away from you as the coder. It becomes a bad thing if you're heavily concerned with performance (games and the likes).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 13 years ago.
For me, No.1 has to be code folding.
#region xxx
#endregion
All of sudden, people find an excuse to have big classes(files) because they can "organize" them nicely. I once saw a half-page-long class with 3,000 lines. I was speechless and they were like "what's the problem"?
No.2 is partial class. It's actually a nice feature especially when you work with generated code. However, some people use it to "break down" a class to multiple pieces. Why? Because that class is so big and popular that it's always locked in the source control. Instead of breaking down the class into different smaller classes, they create multiple files like my_Class1.cs, your_Class1.cs etc so that people can work on Class1 at the same time.
I would like to include "Copy&Paste" but it's not Visual Studio's fault...
Next to #region blocks I would say the ASP.NET IDataSource implementations (SqlDataSource, LinqDataSource) that force (allow) you to write data-access code directly into your aspx pages. I would not use this even for a really small demo app, but nowhere in the documentation of these classes is it mentioned that for a well-designed application you should not use these classes.
I think the most abused feature would be using the default templates as is. Many of the templates include "partial" as well as the regions and include/using statements that aren't required by the code inside of the class.
After that, I'd have to agree with the #region blocks; however, those CAN be useful if done properly. Usually though they are used as a means to organize classes that are larger than they probably should be.
Not paying attention to Warnings
Graphical editor for WebForms. Spoiled so many potentially good developers into drag-and-drop monkeys.