using

How to do C++ style destructors in C#?

I've got a C# class with a Dispose function via IDisposable. Its intended to be used inside a using block so the expensive resource it handles can be released right away. Problem is that a bug occurred when an exception was thrown before Dispose was called, and the programmer neglected to use using or finally. <rant> In C++, I never ha...

How do you resolve .Net namespace conflicts with the 'using' keyword ?

Here's the problem, you include multiple assemblies and add 'using namespaceX' at the top of your code file. Now you want to create a class or use a symbol which is defined in multiple namespaces, e.g. System.Windows.Controls.Image & System.Drawing.Image Now unless you use the fully qualified name, there will be a crib/build error due ...

Uses of "using" in C#

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are good uses of using? ...

Is there a better deterministic disposal pattern than nested "using"s in C#?

In C#, if I want to deterministically clean up non-managed resources, I can use the "using" keyword. But for multiple dependent objects, this ends up nesting further and further: using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReade...

Why should you remove unnecessary C# using directives?

For example, I rarely need: using System.Text; but it's always there by default. I assume the application will use more memory if your code contains unnecessary using directives. But is there anything else I should be aware of? Also, does it make any difference whatsoever if the same using directive is used in only one file vs. most/...

C# "Using" Syntax

Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff } If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it? ...

Response.Redirect("") inside "using{ }"

Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand comm = new SqlCommand(...)) { .. do stuff .. if(condition) Response.Redirect("somepage.aspx"); } } Will the Response.Redirect() exit from the using blocks cause it to dispose all connections? ...

WCF Errors using WCFTestClient to test a simple WCF Web Service

When I try to test the AutoLotWCFService using "wcftestclient", I get the following error. What am I doing wrong? Any insight will help. This is a simple Web Service that has wshttpbinding with interface contract and the implementation in the service. Here is the long error message: The Web.Config file has 2 endpoints - one for Web Servi...

Do you prefer explicit namespaces or 'using' in C++?

When using C++ namespaces, do you prefer to explicitly name them, like this: std::cout << "Hello, world!\n"; Or do you prefer using namespace: using namespace std; cout << "Hello, world!\n"; And if if you prefer the latter, do you declare your usings at file or function scope? Personally I prefer to explicitly name them - it's mor...

What are the benefits of maintaining a "clean" list of using directives in C#?

I know VS2008 has the remove and sort function for cleaning up using directives, as does Resharper. Apart from your code being "clean" and removing the problem of referencing namespaces which might not exist in the future, what are the benefits of maintaining a "clean" list of using directives? Less code? Faster compilation times? ...

C# using statement catch error

I am just looking at the using statement, I have always known what it does but until now not tried using it, I have come up with the below code: using (SqlCommand cmd = new SqlCommand(reportDataSource, new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString))) { cmd.CommandType = CommandType.StoredPro...

Can the "using" declaration be used with templates?

Is it possible to use the "using" declaration with template base classes? I have read it isn't here but is that because of a technical reason or is it against the C++ standard, and does it apply to gcc or other compilers? If it is not possible, why not? Example code (from the link above): struct A { template<class T> void f(T); };...

Does connection close when command is disposed and the connection is defined directly on the command?

I know that a lot of examples exist where a SqlConnection is defined and then a SqlCommand is defined, both in Using blocks: using (var conn = new SqlConnection(connString)) { using (var cmd = new SqlCommand()) { cmd.Connection = conn; //open the connection } } My question: If I define the connection direct...

How does LINQ defer execution when in a using statement

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new MyDataContext) { return dc.tablename.Select(row => row.parameter == a); } } private void UsingFunc() { var result = MyFunc(new a()); foreach(var row in result) { //Do something } } According to the do...

Is it possible to treat a template instance as a namespace?

Suppose I have template< unsigned int num > class SomeFunctionality { static unsigned int DoSomething() { //... } static void DoSomethingElse() { } }; typedef SomeFunctionality<6> SomeFunctionalityFor6; Semantically, "SomeFunctionalityFor6" is essentially a namespace specific to the templ...

Does the using statement keep me from closing or destroying objects?

If I use something like: using (OdbcConnection conn = new OdbcConnection(....)) { conn.open(); my commands and sql, etc. } Do I have to do a conn.close(); or does the using statement keep me from doing that last call? Does it dispose of everything in the using block? For example, if I called other objects unlrelated would it dip...

Refactor "using" statements over an entire codebase?

One of the things I love about Visual Studio 2008 is the ability to refactor and reorganize the "using" statements in source code files (this may have been in 2005 as well, I don't remember). Specifically, I'm talking about how you can have it both reorganize the statements to be alphabetical (though with the core FCL libraries floating...

Does python have something like C++'s using keyword?

In C++ you can often drastically improve the readability of your code by careful usage of the "using" keyword, for example: void foo() { std::vector< std::map <int, std::string> > crazyVector; std::cout << crazyVector[0].begin()->first; } becomes void foo() { using namespace std; // limited in scope to foo vector< ma...

When should I use "using" blocks in C#?

Are there particular instances where I should (or shouldn't?) be using "using" blocks: using(SomeType t = new SomeType()){ ... } ...

What is the best workaround for the WCF client `using` block issue?

I like instantiating my WCF service clients within a using block as it's pretty much the standard way to use resources that implement IDisposable: using (var client = new SomeWCFServiceClient()) { //Do something with the client } But, as noted in this MSDN article, wrapping a WCF client in a using block could mask any errors tha...