How to print my methods name [duplicate] - c#

This question already has answers here:
Can you use reflection to find the name of the currently executing method?
(19 answers)
Closed 6 years ago.
I am new on coding just need find out if it is possible to get methods name and print using console.write. Here I have sample class and I want to grab "myName" so I can use it to print.
using System;
namespace Tests
{
class Class1
{
public void myName()
{
Console.Write(myName);
}
}
}

Simply use the nameof keyword
Console.Write(nameof(myName));

You can get like this for full information:
string method = string.Format("{0}.{1}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, System.Reflection.MethodBase.GetCurrentMethod().Name);
And return this string. Using call this within the metod.
Or short version just for name method, what you want:
string method =System.Reflection.MethodBase.GetCurrentMethod().Name;

Related

Finding a string within another string using C# [duplicate]

This question already has answers here:
Find text in string with C#
(17 answers)
Closed 3 years ago.
How do I find out if one of my strings occurs in the other in C#?
For example, there are 2 strings:
string 1= "The red umbrella";
string 2= "red";
How would I find out if string 2 appears in string 1 or not?
you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string
example :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
You can do it like this:
public static bool CheckString(string str1, string str2)
{
if (str1.Contains(str2))
{
return true;
}
return false;
}
Call the function:
CheckString("The red umbrella", "red") => true

Case Insensitive Interpretation C# [duplicate]

This question already has answers here:
How can I do a case insensitive string comparison?
(9 answers)
LINQ Contains Case Insensitive
(11 answers)
Closed 5 years ago.
I'm making a text game, and I have one method for getting the input from the player and another for processing that input. However, it only works if the player types the command all in lowercase. I want it to ignore case.
public string GetInput()
{
var Test = true;
while (Test)
{
response = Console.ReadLine();
if (validWords.Contains(response))
{
Test = false;
ProcessInput(response);
}
else
{
Console.WriteLine("I'm sorry, I do not understand.");
}
}
return response;
}
public void ProcessInput(string response)
{
switch (response)
{ //Switch statements for responses here
}
}
I've tried using a few other responses I've found here, but they all still only work with lowercase input(using LINQ, string.IndexOf/Equals/etc.). Ideas?
Use string comparer which ignores string case as second parameter for Contains method:
validWords.Contains(response, StringComparer.OrdinalIgnoreCase)
You can make every input you receive, Lower-Case, using ToLower().
Example:
string response = Console.ReadLine().ToLower();
You can add .ToLower() after the readline, as followed:
response = Console.ReadLine().ToLower();
Everything read in from the console will be in lowercase.
You can read more about the ToLower method in the MSDN documentation.
Furthermore, also see the following question if you expect input of certain cultures: string.ToLower() and string.ToLowerInvariant().
Change the text you have to lower using .ToLower().
You're currently using if (validWords.Contains(response)) to check whether the user's response is contained within your validWords list. (I'm assuming).
What you can do is use the following LINQ expression:
if (validWords.Where(w => w.ToUpper().Equals(response.ToUpper())).Count() == 1)
This validates against both upper case strings.

editing injected parameter without returning value c# [duplicate]

This question already has answers here:
When to use in vs ref vs out
(17 answers)
Closed 6 years ago.
I have a question: is it possible to edit the entry of the function without returning any value and those entries will be edited?
Idea:
void AddGreeting(string value)
{
value = "Hi " +value;
}
and calling this function like this:
string test = "John";
AddGreeting(test);
//and here the test will be "Hi John"
is that possible? and how to do it if it is?
You could easily use the ref parameter as so:
void AddGreeting(ref string value){}
and this would do what you want:
void AddGreeting(ref string value)
{
value = "Hi " +value;
}
string test = "John";
AddGreeting(ref test);
Alternatively, you could return a string, which i would consider neater and cleaner to look at

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.)

What does '#' at beginning of a variable mean? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does the # symbol before a variable name mean in C#?
I've seen this a couple of times in code that has been passed onto me:
try {
//Do some stuff
}
catch(Exception #exception)
{
//Do catch stuff
}
Can anyone please explain the purpose of the '#' at the beginning of the Exception variable?
It lets you name a variable using a reserved keyword.
For example:
var #class = "something"; // OK
var class = "something"; // Compilation error
Resharper outputs them sometimes if the name of the variable is close to a class name or a namespace i believe, it is just giving it a unique non clashing name
Shameless rip of Michael Meadows answer to a duplicate question follows.
The # symbol allows you to use reserved word. For example:
int #class = 15;
The above works, when the below wouldn't:
int class = 15;

Categories