String.Empty is not a constant value? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t String.Empty a constant?
To make my code more readable I tried to assign String.Empty to a constant value:
const string PLATYPUS_ADDED_AND_ACCEPTED = string.Empty;
if (false) { }
else
{
toolTip = PLATYPUS_ADDED_AND_ACCEPTED;
}
but I get "the expression being added must be constant"
Isn't String.Empty always the same thing? That seems pretty constant to me.

string.Empty is a readonly field, not a constant.
The compiler has no way to know this will always be the same value.

Related

c# replace character by an empty string [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

C# System.NullException convert null to blank or no value [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
When using the following method there are use cases where one of the parameters, for this example RC_2 (datetime) may pass a null value. I need to have that null value converted into either a blank value or a string value so that it can be passed to a stringbuilder below is what I'm currently using,
public static string CHG_FixDatetime(string RC_1, string RC_2, string seperator)
{
var sb = new StringBuilder();
sb.Append(RC_1);
sb.Append(RC_2);
sb.Append(seperator);
DateTime dt;
if (!string.IsNullorWhiteSpace(RC_2))
sb.append(RC_2)
else
{
sb.Append(Convert.ToString(RC_2));
}
return sb.ToString();
}
I've tried multiple configurations and variations, but I'm unable to get past the System.NullException that is thrown when the RC_2 value is Null. Any help is appreciated.
Try the following solution
RC_2 = String.IsNullOrWhiteSpace(RC_2) ? "" : RC_2;

How to check if a variable is empty in wp7? [duplicate]

This question already has answers here:
Easier way of writing null or empty?
(7 answers)
How can I check whether a string variable is empty or null in C#? [duplicate]
(6 answers)
Closed 9 years ago.
In my app, I want to check if a string variable is empty.
I've handled it as follows,
if ((Name == null) || (Name == ""))
{
//Handled
}
But it passes this condition, if the value is given as " "(whitespace).
How can i detect if the variable contains only whitespaces??
Thanks in advance!
Use String.IsNullOrWhiteSpace:
if (String.IsNullOrWhiteSpace(Name))
{
//Handled
}
String.IsNullOrWhiteSpace(Name)
And MSDN article about it
Use String.IsNullOrEmpty to check null or empty and String.IsNullOrWhiteSpace to check null or whitespace
Use String.IsNullOrEmpty to check if it is null and String.IsNullOrWhiteSpace
to chack for whitespace!! For your case String.IsNullOrWhitespace(Name)

How can I check whether a string variable is empty or null in C#? [duplicate]

This question already has answers here:
Easier way of writing null or empty?
(7 answers)
Closed 4 years ago.
How can I check whether a C# variable is an empty string "" or null?
I am looking for the simplest way to do this check. I have a variable that can be equal to "" or null. Is there a single function that can check if it's not "" or null?
if (string.IsNullOrEmpty(myString)) {
//
}
Since .NET 2.0 you can use:
// Indicates whether the specified string is null or an Empty string.
string.IsNullOrEmpty(string value);
Additionally, since .NET 4.0 there's a new method that goes a bit farther:
// Indicates whether a specified string is null, empty, or consists only of white-space characters.
string.IsNullOrWhiteSpace(string value);
if the variable is a string
bool result = string.IsNullOrEmpty(variableToTest);
if you only have an object which may or may not contain a string then
bool result = string.IsNullOrEmpty(variableToTest as string);
if (string.IsNullOrEmpty(myString))
{
. . .
. . .
}
string.IsNullOrEmpty is what you want.
Cheap trick:
Convert.ToString((object)stringVar) == ""
This works because Convert.ToString(object) returns an empty string if object is null. Convert.ToString(string) returns null if string is null.
(Or, if you're using .NET 2.0 you could always using String.IsNullOrEmpty.)

C# get string name's name [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get variable name using reflection?
How to get the string name's name not the value of the string
string MyString = "";
Response.Write(MyString.GetType().Name);//Couldn't get the string name not the value of the string
The result should display back the string name "MyString"
I've found some suggested codes and rewrite it to make it shorter but still didn't like it much.
static string VariableName<T>(T item) where T : class
{
return typeof(T).GetProperties()[0].Name;
}
Response.Write(VariableName(new { MyString }));
I am looking another way to make it shorter like below but didn't how to use convert the current class so i can use them in the same line
Response.Write( typeof(new { MyString }).GetProperties()[0].Name);
While Marcelo's answer (and the linked to "duplicate" question) will explain how to do what you're after, a quick explanation why your code doesn't work.
GetType() does exactly what it says: it gets the Type of the object on which it is called. In your case, it will return System.String, which is the Type of the object referenced by your variable MyString. Thus your code will actually print the .Name of that Type, and not the name of the variable (which is what you actually want.)

Categories