How to print function name without hard code string? [duplicate] - c#

This question already has answers here:
How do I get the current line number?
(7 answers)
Closed 2 years ago.
c language has macro like FILE/LINE to print file name and code line number
So how does C# language achieve same goal, is it possible to do, or require assembly packages?
Thanks a lot.

You can use StackFrame, i would be wary about doing this though, it will come at a performance cost and is likely to give you issues in release .
Represents a stack trace, which is an ordered collection of one or
more stack frames.
var stackTrace = new StackTrace(0, true);
var sf = stackTrace.GetFrame(0);
Console.WriteLine("FileName: {0}", sf.GetFileName());
Console.WriteLine("Line Number:{0} ",sf.GetFileLineNumber());
Console.WriteLine("Function Name:{0}",sf.GetMethod ());

Related

What means the local _ in C#? [duplicate]

This question already has answers here:
C#7: Underscore ( _ ) & Star ( * ) in Out variable
(6 answers)
Discard feature significance in C# 7.0?
(7 answers)
Closed 3 years ago.
First of it all: my C# Knowledge is depricated because i do other projects in the meantime.
So i come back and see whats up now, but there is one thing i do not understand.
This is now valid in C#
_ = new object();
I try to use a search engine of my choice but u can imagine search for "C# _" isnt very helpful to search for. Can someone explain whats going on here? My idea was this is placeholder to pop the result back on the stack and avoid agressive compiler optimization. But at the end, i have no idea.

Convert from %20 to normal character (replace) [duplicate]

This question already has answers here:
decode percent-encoded string c# .net
(4 answers)
Closed 7 years ago.
I've searched for this and cannot find any hint anywhere.
Basically there is a program that formats their file name like this
hello%20world%28hello%20world%29
which is supposed to mean hello world(hello world)
now im wondering is there any way that I could read every file name and anything that uses "%ascii" would be converted to normal text (e.g above).
Thanks in advance guys. I'm not that experienced in code and I'm hoping that someone could help me.
Use System.Uri.UnescapeDataString:
Uri.UnescapeDataString("hello%20world%28hello%20world%29");
Prints:
hello world(hello world)

Why string.format? [duplicate]

This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format

c# stringbuilder [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use StringBuilder?
I am working hard in performance gaining in the Enterprise project. Our solution architect suggesting to convert all string declarions in to stringbuilder If even one append is there to that string variable. for ex: str += str2;
I know, if we are modiying string many times (ex: many appends) can be time consuming to create new string objects. we should go to stringbuilder.
Now Question is -
Creating string builder object (without specifying default size) Vs one time append in to string variablein respect of performance improvement.
There are a number of articles out there that answer this question for you. Here is the first one I came across:
http://www.codeproject.com/Articles/6771/String-Vs-StringBuilder-C
Basically, the answer is that no, you probably shouldn't use stringbuilder for instances where you only append text once or twice. It is really for when you are doing so repeatedly.

Realtime complination in C# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there a method that will evaluate a string and produce an integer (assuming the string is an equation) in C#
Hi all, i just wonder how to make a realtime complination in C#
For example: i have a string like this
string math = "1 + 2 + (4 - 6)";
And i want to complie it to get the result
How to do that? and is that the bad idea because i want to make a calculator in C#?
Edited:
The main question properly is that i want to do it in WP7, not exactly in C# windows lol, i tried all the solutions below but not at all is correct!
and is that the bad idea because i want to make a calculator in C#?
One problem with that is that your calculator language is probably supposed to be just a subset of C#. So using the C# compiler may be too flexible and allow arbitrary C#. Kind of like problems with SQL injection attacks.
Expresion Evalution is an application of STACK (Data Structure)
You can see these link If you want Sample projects
http://www.vbforums.com/showthread.php?t=397264
http://www.codeproject.com/KB/cs/runtime_eval.aspx
http://www.c-sharpcorner.com/uploadfile/mgold/codedomcalculator08082005003253am/codedomcalculator.aspx

Categories