You will need Visual Studio 2012 or higher. (Released September 2012). .NET Framework. 4.5.
class Program
{
static void Main(string[] args)
{
var p = new Program();
p.Run();
}
void Run()
{
var task = CalculatePiAsync();
task.Wait();
}
async Task CalculatePiAsync()
{
var task = new Task(CalculatePi);
task.Start();
await task;
}
void CalculatePi()
{
Thread.Sleep(2000);
}
}
Caller info attributes provide information about the caller of a function.
public void DoTest1()
{
LogMessage("hello");
}
public void LogMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Console.WriteLine("message: " + message);
Console.WriteLine("");
Console.WriteLine("CallerMemberName: " + memberName);
Console.WriteLine("CallerFilePath: " + sourceFilePath);
Console.WriteLine("CallerLineNumber: " + sourceLineNumber);
}
message: hello
CallerMemberName: DoTest1
CallerFilePath: C:\...\Test1.cs
CallerLineNumber: 20
You can use [CallerMemberName] to implement INotifyPropertyChanged.
public event PropertyChangedEventHandler PropertyChanged;
int _count;
public int Count
{
get { return _count; }
set
{
_count = value;
NotifyPropertyChanged(); // explicit property name "Count" not needed
}
}
void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}