Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 PropertyChanged?.Invoke(this, args);

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 PropertyChanged?.Invoke(this, args);

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 PropertyChanged?.Invoke(this, args);

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

Commonmark migration
Source Link

###OnPropertyChanged()

OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 
 PropertyChanged?.Invoke(this, args);
 

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

###OpenMarkDown() and OpenCss()

OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

###OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 
 PropertyChanged?.Invoke(this, args);
 

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

###OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 PropertyChanged?.Invoke(this, args);

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).

Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

###OnPropertyChanged()

This method to raise the PropertyChanged event needs only to be called if the value is changed which isn't verified by the setters of your properties yet. To fix this issue and prevent unneeded work to be done a simple if condition is needed like so

public string MarkdownContent
{
    get { return _markdownContent; }
    set
    {
        if (_markdownContent == value) { return; }

        _markdownContent = value;
        OnPropertyChanged(new PropertyChangedEventArgs(nameof(MarkdownContent)));
        UpdateHtml();
    }
}

The implementation of the OnPropertyChanged() can be improved by just using the ? null-conditional operator which is clearly stated in the New Features in c# 6

We expect that a very common use of this pattern will be for triggering of events:

 PropertyChanged?.Invoke(this, args);

This is an easy and thread-safe way to check for null before you trigger an event. The reason it’s thread-safe is that the feature evaluates the left-hand side only once, and keeps it in a temporary variable.

###OpenMarkDown() and OpenCss()

You have duplicated code here and an unused method parameter. By introducing a string GetLoadFilename(string filter) (not sure about the method name) this can be prevented like so

private string GetLoadFilename(string filter)
{
    var dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.Filter = filter;
    var result = dialog.ShowDialog();
    
    return result.Value ? dialog.FileName : string.Empty;
}

and now for instance OpenCss() would look like so if we also extract the actual reading of the file to a string ReadFile(string) method

public void OpenCss(object parameter)
{
    var fileName = GetLoadFilename("CSS Files|*.css|All Files|*.*");
    if (fileName.Length == 0) { return; }

    CssContent = ReadFile(fileName)

}

private string ReadFile(string fileName)
{
    using (var sr = new StreamReader(dialog.FileName))
    {
        return sr.ReadToEnd();
    }
}  

Almost the same refactoring should be applied to the SaveMarkdown(), SaveCss(), SaveGeneratedHtml() and SaveRenderedHtml() by introducing the methods string GetSaveFilename(string) and void SaveFile(string).