Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
experts, can you please explain me this code.


C#
public partial class _Default: System.Web.UI.Page
{


C#
private static readonly string _connString = String.Empty;
   SqlConnection con = new SqlConnection(_connString);



and also this code............

C#
static _Default()
    {
        _connString = WebConfigurationManager.ConnectionStrings["UsersDBConnectionString"].ConnectionString;
    }


so, what is meant by string.empty

Please explain.
Posted
Comments
Mohan Gopi 5-Sep-13 2:49am    
string.empty means string contain empty or null or "" values.
lukeer 5-Sep-13 3:48am    
Not null[^], just the others.

C#
public partial class _Default: System.Web.UI.Page

Defines one class named _Default that inherits from System.Web.UI.Page. The partial keyword instructs the compiler to not generate errors for not finding at this scope the entire declarations and definitions for the class _Default. The rest of the class should be provided in another similar declaration (using the partial keyword also).

C#
private static readonly string _connString = String.Empty;

Declares one private (scoped to the class, invisible out of there) field which can not be written (that's the meaning of readonly). The static keyword specifies that this field will be global (shared by) to all instances of that class.
Code writing to static fields is not reentrant, take care with that.
One thing else, the fact that it is marked readonly implies that it can not be changed, so it makes no sense to initialize it with the value String.Empty if it will be used later like your code does.

SqlConnection con = new SqlConnection(_connString);

This creates one instance of the class SqlConnection(). Take a look at this, as _connString can not be modified and has been initialized with an empty string... presumably you'll not be able to establish the connection with this code.

Perhaps you should consider removing static and readonly from your _connString declaration.
 
Share this answer
 
The string.Empty field is an empty string literal.

check this link
 
Share this answer
 
v2
In application code, this field is most commonly used in assignments to initialize a string variable to an empty string. To test whether the value of a string is either null or String.Empty, use the IsNull Or Empty method.
 
Share this answer
 
The string.Empty field is initialized to "" at runtime by the .NET Framework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900