I want to be able to add my own functions and variables to the existing string variable.
Such as instead of
if(string.IsNullOrEmpty(mystring) == false)
I do this
if(mystring.isEmpty == false)
With isEmpty's get just returning isnullorempty().
This is just one of many functions I need to add to this variable to speed things up.
note* string not String
You'll want to use extension methods. But be careful not to make them act differently from normal methods.
Use extension method.
Create a static class and then declare static method(extension methods) on string like this
//this indicates you are extending method in string class
public static bool isEmpty(this string input)
{
//your logic
}
All the linq queries have been implemented as extension methods
You need to implement extension method like below :
public static bool isEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
You can enhance every type with extension methods. But unfortunately you can only write methods, properties can not be added to a type. So if(mystring.isEmpty == false) of your sample is only working with a method like this if(mystring.IsEmpty() == false)
Thats not a function, thats a property
You can create new extension methods for string, but there arent extension properties in C# 4
There is no difference between string and String
In the .Net World isEmpty would start with a capital letter
Instead of writing if (someBool == false) you should write if (!someBool)
I highly question any speed improvement that you hope to see here. VS has great IntelliSense features you should learn and use those.
Related
I am writing a simple C#.NET application where I have an if condition where I am checking if a string variable value is a string or another string or another string or another one, etcetc.
Something like this:
if(protocollo == "2019/0002391" || protocollo == "2019/0002390" || protocollo == "2019/0001990" || ........)
This solution works but it is not so elegant. What could be a smarter way to implement the same behavior?
I agree with #JeroenMostert that it really depends on the context of the rest of your application. That said, using an array of strings and checking if your string is in there is a nice straightforward solution. There are certianily solutions that would scale better, take a look at HashSet.
string[] s = new string[] { "2019/0002391", "2019/0002390", "2019/0001990", ... };
if (s.Contains(protocollo)) {
// fill in here
}
You never said, so I'm making the assumption that the strings you're checking against is hard-coded and not something that changes often. To that end, you could create a string[] or HashSet<string> in a static class so it only initializes the one time, then expose a method for checking a second string against the valid ones.
void Main()
{
Console.WriteLine(Protocols.ValidProtocol("2019/0002391")); //True
Console.WriteLine(Protocols.ValidProtocol("2018/0000000")); //False
}
// Define other methods and classes here
public static class Protocols
{
public static bool ValidProtocol(string protocol)
{
return _validProtocols.Contains(protocol);
}
private static readonly HashSet<string> _validProtocols = new HashSet<string>
{
"2019/0002391",
"2019/0002390",
"2019/0001990"
//etc, etc...
};
}
A solution like this would probably not be ideal if the list of strings you need to check against changes often. You'd probably want to pull the list from an external source like a file or a database if you need to modify it often.
I had some code similar to your example in a static extension method. I didn't want to have to instantiate an array every time the method was called, but I wanted to improve the readability of the code.
I improved the code using the switch expression which was added in C# 8. Here is the what your example might look like if implemented with a switch expression. Depending on what your code does if the condition is true you may be able to improve on this, but this is the basics.
var isProtocolloMatch = protocollo switch
{
"2019/0002391" => true,
"2019/0002390" => true,
"2019/0001990" => true,
_ => false
};
if (isProtocolloMatch)
{
// do stuff
}
I have a class StringFormatter which contains method RemoveCharFromString.
For a long time, I have been creating a new instance of a class and then use it like the following:
[...]
StringFormat sf = new StringFormat();
string exampleString = sf.RemoveCharFromString(inputString, '%');
[...]
Now I came to a point where I just have to use this method a single time in one class. I thought there might be a shorter way of accomplishing the above code such as:
[...]
string exampleString = new StringFormat.RemoveCharFromString(inputString, '%');
[...]
Is there something for that?
You can instantiate a class and call one of it's methods directly - your second code sample just needs a parenthesis after the constructor:
string exampleString = new StringFormatter().RemoveCharFromString(inputString, '%');
However - there are things to consider here, without knowing the insides of the method:
The method's name suggests it's basically removing a specific char from the string - If it removes all occurrences of said char, why not just use string.Replace()?
Since this method seems to be getting all the information it needs from it's arguments and does not rely on, nor changes the state of the StringFormatter instance, why not make it a static method?
Sounds to me like the StringFormatter class is a bunch of methods which works on the type string. One option, could therefore be to consider to use extensions methods on the string type instead
public static class StringFormatter
{
public static string RemoveCharFromString(this string value, char charToRemove)
{
//do your logic and then return a string
}
}
Then use it
var exampleString = inputString.RemoveCharFromString('%');
Class example:
public class SomeType
{
private int type;
// some code...
public override string ToString ()
{
if (type == 1) return "One";
if (type == 2) return "Two";
}
}
Now imagine application calls thousand times ToString() method in a second.
My question is: when I use inline created string in code like something = myClass.ToString() is in every call created a new string or compiler optimize it somehow? (because strings are immutable it could be returned only referense to a static string).
And if not, should I make static private string fields and return them in ToString method for performance reasons?
Ofcourse I will test it using Stopwatch, but I need an expert answer anyway.
You're using string literals - which means you're returning a reference to the same string each time. This is guaranteed by the language specification. From section 2.4.4.5 of the C# 5 specification:
When two or more string literals that are equivalent according to the string equality operator (ยง7.10.7) appear in the same program, these string literals refer to the same string instance.
So as a simpler example:
string x = "One";
string y = "One";
Console.WriteLine(object.ReferenceEquals(x, y)); // Prints True
In your code, the ToString() method will still be called - but it won't create a new string object each time. You might consider using a switch statement instead of all those if statements, by the way.
Note that even if it did create a new string each time, creating thousands of strings per second won't make a modern CPU break into a sweat. Both the allocator and garbage collector are pretty efficient, and modern computers can do an awful lot of work in a second.
I find myself often needing to use int.TryParse() to test if a value is an integer. However, when using TryParse, I have to pass a reference variable to the function. So I find myself always needing to create a temp int to be passed in. Usually it looks something like:
int my_temp_integer;
int.TryParse(potential_integer, my_temp_integer);
I find this to be quite cumbersome considering that all I want is a simple true/false response, and I don't care about the actual parsed result. Is there a better way to approach this? Why isn't there an overloaded function where I can just pass the value I want to test and get a true/false response?
Thanks.
you could write an extension method:
public static bool IsInt(this string pString)
{
int value;
return int.TryParse(pString, out value);
}
then your example becomes:
potential_integer.IsInt();
EDIT:
Lately I have been using a generic form of this.
public delegate bool TryParser<T>(string pString, out T pResult);
public static bool Is<T>(this string pString, TryParser<T> pTryParser)
{
T val;
return pTryParser(pString, out val);
}
Can then use it as follows; it's not perfect, but it's more concise than anything I've found:
"1234".Is<int>(int.TryParse); // true
"asdf123".Is<int>(int.TryParse); // false
"1.2345".Is<float>(float.TryParse); // true
"1000".Is<byte>(byte.TryParse); // false
Theoretically, this would also work with custom TryParse methods, as long as you followed the same pattern as the official ones.
Update: If you maintain a static dictionary of TryParse methods by type, you can avoid having to ever directly pass the method. The dictionary can even be populated as needed with reflection.
A simple solution is to create an extension method.
public static class StringExtensions {
public static bool IsInt(this string s) {
int i; return Int.TryParse(s, out i);
}
}
Then you just use it as so:
string s = "123";
if (s.IsInt())
// do something.
if you don't want to actually convert the string, only test it, then you can use Regex
something kinda like this (you may need to adjust this to fit your needs):
public bool IsInt(this string inputData)
{
Regex isNumber = new Regex(#"^\d+$");
Match m = isNumber.Match(inputData);
return m.Success;
}
You could use
bool isInt = str.TrimEnd( new char[] {'0','1','2','3','4','5','6','7','8','9'})
.Length == 0;
A shorter alternative to Muad'Dib above:
bool IsInt(string input)
{
return new System.Text.RegularExpressions.Regex(#"^\d$").IsMatch(input);
}
/Hans
I currently have a method that removes duplicates from a line, although the output is currently going into a new file. How could I instead use the output as a string and then use it in another method:
Well, you can store the return value of the first method in a variable and pass it to the second method (after modifying the second to accept an argument):
var str = RemoveDuplicate(someValue, someOtherValue);
CompareFiles(str);
The second method would need to be defined with an argument:
private static void CompareFiles(string someValue)
{
// implementation
}