|
Thanks for your particular answer It helps me a lot
|
|
|
|
|
This is a bit confusing, I've seen the using keyword used both before and after the Namespace.
using System;
using System.Data;
namespace MyCompany
{
public MyClass {}
}
as well as
namespace MyCompany
{
using System;
using System.Data;
public MyClass {}
}
I've used both and cannot find a difference in actual performance. Is this something that is purely for personal choice or is there some sore of advantage to using one over the other.
|
|
|
|
|
it makes not different most of the time. however if you have more than one namespace in your file it makes different.
namespace MyCompany
{
using System.Data;
}
namespace OtherCompany
{
... can't use System.Data directly here.
}
|
|
|
|
|
Sweet thanx. I don't know why I've adopted putting them inside the namespace, I was happy with them outside and then suddenly I decided to put them in for no apparent reason .
|
|
|
|
|
Not that you are or aren't suggesting the practice of having more than one namespace in a single class file, but I'd have to say...DON'T!!!
You should never have more than one namespace within a class file. Honestly, you should never have more than one class in a class file. This is why it's referred to as a class file.
Of course, this is just an ideal. But, sticking to it will lessen problems in the future...trust me.
Michael Flanakin
Web Log
|
|
|
|
|
|
v3ct0r wrote:
I've used both and cannot find a difference in actual performance
Its not a performance thing. using is mearly telling the compiler that you will be using stuff from other namespaces implicitly (i.e. without having to give a fully qualified name like System.Data.SqlClient.SqlConnection )
If there is any performance gains to be had it will only be at compile time and not during runtime.
The other reply you got explains the rest, so I won't repeat it.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
As you've seen from everyone else's comments, you can put your using statements in or outside of the namespace declaration. Now, to caveat that, I'd like to highly suggest that you put them outside of it. Most code that you see should have the using statements at the top of the class file, so I'd just stick with this standard. As if industry use wasn't enough, it's actually Microsoft's suggested method.
Flexibility is nice, but there are times when you should choose standards over that flexibility. I think that this is one of those times.
Michael Flanakin
Web Log
|
|
|
|
|
Thank you. I agree with you, I'll be pulling my using statements to the top of the page.
Funny thing however, Microsoft had a Direct X 9 sample that had the using statments inside the namespace, this is actually where I picked it up from. Perhaps there was a specific purpose that I overlooked.
Anyway, thanks everyone else as well.
|
|
|
|
|
I'm sure this is a case of every developer has their way. What "Microsoft" suggests isn't always the same as what their developers do.
Michael Flanakin
Web Log
|
|
|
|
|
Ok, simple question, but I can't for the life of me figure out WHY this is happening.
I have a simple (right now anyway) Button-style control derived from System.Windows.Forms.Control. I have two custom properties and both have the DesignerSerializationVisibility attribute set to Visible. This *seems* to work (although I can't figure out why setting it to Content seems to make them NOT persist) However when I drop a few of these buttons on to a form, at first all is good - they look correct in design-mode, etc. As soon as I save and compile/run it though at least one of the controls (always the last one for some reason) disappears in design-mode. When the application is run, all is fine and all buttons are shown but coming back to design-mode yet again that last button is gone.
When I look through the windows-form-generator code in the form itself, I see that it IS setting the location and size of all the buttons correctly, yet when I highlight the *problem-disappearing* button in the form's property editor, it gets focused and is in the very upper-left of the form with a size and location both of 0,0.
Any ideas just what I'm seeing/doing here? Its driving me nuts!!!!
Thanks!!
|
|
|
|
|
I recently took a crappy C# test and wasn't sure about the correct answer to four of the questions (below). If you know the correct answer to any of them (no guessing please), please tell me and I'd also like a brief explanation if possible.
1. What is the purpose of the definite assignment in C#:
A. You must initialize every variable in a program
B. You must assign a value to every variable in a program
C. You must at least assign a zero to every variable in a program
D. You must assign a value to a variable before you use it
2. Which of the following statements is FALSE:
A. On disk, assemblies are Portable Executable files.
B. Assemblies are loaded on demand.
C. Assemblies will not be loaded if not needed.
D. Assemblies are always loaded.
3. There are two kinds of attributes in C#:
A. instrinsic and custom
B. internal and external
C. getters and setters
D. push and pop
4. Which of the following statements is FALSE:
A. Reflection can be used to examine the contents of an assembly.
B. Reflection can be used to find the types associated with a module.
C. Reflection can be used to find the events associated with a type.
D. Reflection can be used to find the types base class.
E. Reflection can be used to find the types associated with a namespace.
Thanks!
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
No offense meant, but I don't think anyone will help you here, nor should they. If you have specific questions regarding C# we'd be glad to offer help, but we won't do your homework for you.
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
Judah H. wrote:
but we won't do your homework for you.
He is older than that to have homework.
Mazy
"Man is different from animals in that he speculates, a high risk activity." - Edward Hoagland
|
|
|
|
|
Thanks Mazy, us senior folks just don't get any respect any more.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
Dude, I think you've been exposed to too many "do my homework for me" posts disguised as something else.
I'm being completely honest about my post, and I have published a number of articles on this site that demonstrate that I don't do homework anymore. Now, if you can answer any of my 4 questions, which by the way are quite specific, I'd appreciate it.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
Judah H. wrote:
we won't do your homework for you
It doens't look like homework to me. It was clear to me that the test in question has happened and is in the past therefor any answers gained here are irrelevant to the result of the test.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
|
|
|
|
|
4. Which of the following statements is FALSE:
A. Reflection can be used to examine the contents of an assembly.
B. Reflection can be used to find the types associated with a module.
C. Reflection can be used to find the events associated with a type.
D. Reflection can be used to find the types base class.
E. Reflection can be used to find the types associated with a namespace.
A. this answer is not really descriptive enough. What do they mean by content of assembly?
If it's type information, then True.
B. TRUE. Module.GetTypes()
C. TRUE. Type.GetEvents()
D. TRUE. Type.BaseType
E. Can't be derived easily, but you can go thru all loaded assemblies, and thru all types and get the namespaces. But you won't get all, as you can define types to belong in a namespace in a different module that's not event loaded.
|
|
|
|
|
Alvaro Mendez wrote:
1. What is the purpose of the definite assignment in C#:
D. You must assign a value to a variable before you use it
You'll get warnings that say this from the compiler itself, if you try to use a variable before it has been assigned.
Alvaro Mendez wrote:
2. Which of the following statements is FALSE:
I know that A is true. If D was true, then B and C would both technically be false, so I would say that D is false. (Another helpful bit of info is that the JIT doesn't generally compile code until it needs to run it. So, until code that would need to reference an not-yet-loaded dll is called, that dll shouldn't be loaded.) (Also! There are way too many ways to load an assembly dynamically whenever you feel like it for B and C to really apply at all unless they are talking about default behavior of the CLR.)
Alvaro Mendez wrote:
3. There are two kinds of attributes in C#:
Assuming that "attributes" means the classes derived from System.Attribute, then A would be the only applicable answer, I think. B is possible, but would apply to basically everything in the .NET Framework if that was the correct answer. C is totally irrelevant, since that would be about Properties. D is obviously related more to a stack than to the C# language.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
John Fisher wrote:
You'll get warnings
Errors! Not warnings A pain in the ASS.
top secret
|
|
|
|
|
1. D //you dont even wanna see my bitching in my code about this!
Short explaination (csc complains):
int l;
if (somebool)
{
l = 0;
}
if (somebool)
{
COnsole.WriteLine(l);
}
2.A (they are an extention to PE files),D (see B & C)
3.I would say A, as the rest is even more absurd!
4. None (maybe A, they dont define contents)
top secret
|
|
|
|
|
I agree with most of what you said, and for an explanation of (3), go here: http://www.ondotnet.com/pub/a/dotnet/excerpt/prog_csharp_ch18/index.html?page=2#attributes
But for number 4, I believe E is the one that's false. How could you use reflection to find the types associated with a namespace? I'm not terribly familiar with C# reflection, since I've only done a small amount work with it, but I couldn't find any methods or classes in the reflection namespace which would allow for investigating namespaces specifically. Since assemblies are loaded only when needed (or explicitly loaded), most types in any given namespace wouldn't be available to reflection anyway.
Reflection can be used to identify the types associated with a namespace in one specific assembly, but without already knowing exactly which assemblies contain types within a specific namespace, how would one really list all the types associated with a namespace?
|
|
|
|
|
Curi0us_George wrote:
But for number 4, I believe E is the one that's false. How could you use reflection to find the types associated with a namespace?
You can enumerate all types in all assemblies and compare namespaces with the type you're searching for. The reflection classes don't have any methods that will get you types for a namespace, but it is possible.
And just because types aren't JIT'd until they're used doesn't mean reflection can't see them. It can get all the type information without actually loading and JIT'ing the type because it uses the metadata in the assembly.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How would you go about enumerating all assemblies? And if you're going to say that you can manually walk the path, I'd say you're no longer just doing reflection. At some point, you're tacking on too much functionality to legitimately call it a part of the feature set.
|
|
|
|
|
Enumerating assemblies would be application-specific. You could enumerate all loaded assemblies (AppDomain.GetAssemblies ), load and enumerate all assemblies in a directory, etc.
To what degree you can call this reflection is a matter of opinion, though. You're still using reflection to gather this information and only simple collection storage and enumeration plus string comparisons to compare, but you wouldn't be there without reflection. More than any answer, I agree this is the wrong answer for that question but in my opinion they're all right. Too many times I've seen tests made up by people that really don't know the technology as well as they should before considering themselves an authority.
Microsoft MVP, Visual C#
My Articles
|
|
|
|