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 9 years ago.
Improve this question
I work in a code base that is quite large and today I found a project that was emitting IL code inside a normal class.
The project containing the IL code being emitted was a implementation of a Service Locator MSDN Desctiption.
What are the advantages of doing this and why would this be done as apposed to using the C# language?
Typically this is done to circumvent the overhead of using reflection, using information only available at runtime.
You would then use reflection, which can be slow depending on what you do, to build a new piece of code that works directly with the data given to it, without using reflection.
Advantages:
Performance
Disadvantages:
Hard to debug
Hard to get right
Hard to read code afterwards
Steep learning curve
So you need to ensure it's really worth the price before embarking on this.
Note that this is a general answer. In the specific case you came across, there is no way to answer why this was done nor which particular advantages (or disadvantages) you would have without actually seeing the code.
There are many uses for this.
One of the more often used scenario is for changing/injecting code on the fly:
.NET CLR Injection: Modify IL Code during Run-time
A good tutorial that help me to understand a good use for it is:
Dynamic... But Fast: The Tale of Three Monkeys, A Wolf and the DynamicMethod and ILGenerator Classes
Good luck
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 7 years ago.
Improve this question
In C#, how expensive it is to create a new class instance?
I'm speaking in context of using C# in unity3d. Meaning that stuff continuously gets called many times per second.
In C++, generally speaking (while making games) you may want to reuse anything you created with new/smart pointers, you would want to keep allocated resizeable buffers/lists/fifos and you may want to avoid that uses dynamic memory allocation (and stick to local variables) if code is getting performance critical.
So, what is the recommended way to do it in C#? Is it a very bad idea to create a new List, return it from the function and then "forget" about it, never using it again?
P.S. I'm aware of profiling and "premature optimizations", but I'd like to know some generic guidelines for the language before I (possibly) make a big mess because I used the wrong approach.
I remember reading a fun post Performance numbers in the pub by Ayendy Rahien.
How many CLR objects can you create in one second?
And here was the result back in 2011
Created 7,715,305 in 00:00:01
Jokes aside. Create is pretty cheap operation but GC is not. So while you can create really many objects, it is the collect that will hurt performance. So a rule of objects reuse can apply to C# as well.
I'd assume, but can be wrong, implementation of a new operator is located in JIT
aloc.h
aloc.cpp
As usual, avoid premature optimisation till you need it.
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
I am making a game in C# and XNA and will be porting it using MonoGame. I would like to know which is a better option for performance.
Should I write and type out my maps in the code of the game itself, or should I create an XML file and store it in their?
By map I mean the layout of the tile-map. It looks like this if I type it in the code:
map.Generate(new int[,]{
{0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,0,0,0,},
}, 64);
I am new as a programmer and any advice should help?
Thanks, BlazeCrate
There will be neglagable performance difference between the two as no matter which you use they both will end up being stored as some kind of in memory object. The only potential difference in "performance" is how long it will take to make that in memory object once at the start of the level loading (for something that simple it would only take a few ms extra to load, likely unnoticeable)
Do whatever is easier for you to implement and develop for.
Doing it in XML would allow you to design an editor, so that you can use your own GUI to design your content, allowing you to more easily generate much more complex content. It also allows you to modify your content without recompiling your game.
That said, it depends on the scale of your project, and your goals. You should choose the simpler approach if possible, if you want to eventually release it. If you choose the more complex yet more scalable approach, you run the risk of making the project too complex to finish. If your goal is to eventually release, then stay as simple as possible, but if your goal is more along the lines of learning to be a good software engineer, then choosing the more complex approach could be the way to go.
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 9 years ago.
Improve this question
I asked about this Q format on meta, and the they said that worded correctly, this should be appropriate. This being said, sorry if I still butchered the wording and just in case I'd like to get this across (as was recommended): I'm not looking for opinions on certain things or how they work, rather just the ways it's possible. I don't mean to sound ignorant and I'm truly sorry if I do, it was just suggested to me to say on Meta.
Now for the Q; What just general language at all would be capable of gathering information from public online websites, then putting it in the program where it could be further processed as just any old variable? I'm new to coding and wanted to do this as a little 'introductory' program, to teach myself some new stuff. Problem being, with my idea, I don't even know where to start. Again, I'm not asking for specific ways to do this, I was just curious what languages are capable of doing this at all? I'd prefer to do it in a Visual Studio's language (no preference of which ones), if that's possible.
In short: Are either Visual C#/C++ capable of gathering information online to be further handled within the program? If not, what languages are?
I agree with the comment that this is a complicated first programming task. However, you'll undoubtedly learn something trying it.
If you already had some experience programming in Python, I'd suggest you took a look at http://scrapy.org/doc/ which is a framework (that is, a bunch of classes and other useful tools) which let you write programs to extract information from web pages. Scrapy does let you concentrate on programming by taking care of some of the nasty details involved in parsing web pages.
Another option is to use a javascript framework, maybe something like node.js.
I've done a fair amount of web scraping, and I usually end up using a combination of utilities which clean up web pages and a variety of XSLT processors. I personally find that combination of technology to be easier to deal with; I don't try to use C-family languages until I've basically wrestled the data into shape. But everyone has their own style.
Good luck!
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 9 years ago.
Improve this question
Recently I have been asked in an Interview that: "Can you give an example of a situation where is it necessary to break the Inheritance chain?". I am not very sure if I could follow the question exactly.
Does any such situation exist when we to break the inheritance chain?
I tried google, but didn't get any clues.
A. When we get stupid questions that make no sense.
Inheritance is just a tool for managing and re-using code. Composition is a strong tool that is not part of an "inheritance-chain" so I'm guessing that's an answer they're looking for?
Another possible answer they're looking for is utilizing interfaces. As interfaces don't require an "inheritance chain". They enable you to be a little more flexible with your architecture and step away from strict inheritance "chains".
However the question implies that you have a number of objects that all inherit from one another and for some reason you "break" the chain of inheritance somewhere. There is no "set" reason why you'd do this as each implementation of OOP that addresses a problem is typically unique.
The way the interviewer phrased the question makes little to no sense. It's a bad interview question that wont result in the best answers or necessarily tell you anything about a candidate except that they don't understand your madness either ;).
EDIT: added some "better" questions.
Better questions include:
Q. What is the difference between inheritance and composition?
Q. I have the following class model (one crying out for an interface), can I improve it at all?
Q. I'm re-designing a base class and want to prevent other people from overriding this function. Can I do that?
Q. Is there a problem with calling virtual methods in class constructors, if so, what?
There's this blog post with a good explanation on why you'd want to "break the inheritance chain" (or "seal" your class).