C # using statement more understanding

Can someone help me understand the following code

public Font MyFont { get; set; } void AssignFont() { using (Font f = new Font("Shyam",2)) { this.MyFont = f; } } 

Can I assign a utility object to the MyFont property?

+6
source share
2 answers

Although it may be "valid for assigning a MyFont object to an object", the object can no longer be useful because it can free managed and / or unmanaged resources. Objects created by opening the using statement indicate that the class underlying the object implements the IDisposable interface. This should be considered a warning sign that when placing an object, you should stop interacting with it, including maintaining a link to it.

In the case of a font, the font underlying the resources was removed when you left the usage block:

 using (Font f = new Font("Shyam",2)) { this.MyFont = f; } 

This is easy to verify if you try to use a font in any drawing operation.

This code will fail with a System.ArgumentException because the font is located:

 public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } private void UserControl1_Load(object sender, EventArgs e) { if (DesignMode) { return; } AssignFont(); } #region Overrides of Control /// <summary> /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"/> event. /// </summary> /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data. </param> protected override void OnPaint(PaintEventArgs e) { try { var g = e.Graphics; g.FillRectangle(Brushes.White, e.ClipRectangle); g.DrawString("Hi there", MyFont, Brushes.Black, 0, 0); // <--- this will fail } catch (Exception ex) { Trace.TraceError(ex.Message); } } #endregion void AssignFont() { using (Font f = new Font("Shyam", 2)) { this.MyFont = f; } // <---- MyFont now points to a disposed object } public Font MyFont { get; set; } } 

enter image description here

The problem with your code is that you highlight something in the usage block and keep the link to it in another place. In your scenario, because you want to use the font elsewhere, it makes no sense to have a using block.

Bad:

 using (Font f = new Font("Shyam",2)) { this.MyFont = f; } 

It is better:

 this.MyFont = new Font("Shyam",2) 

Fonts I suspect use native fonts, hence the resource.

+9
source

General rule: Do not use a disposable object outside the using block. In most cases, disposable items are unsuitable for use after disposal. If you follow this rule, you will run into less problems.

In this particular case, I would not recommend using , since you expect to use a font object outside of the AssignFont method.

So, when did you choose the font? One way: you can remove the font in the Disposed event Disposed parent control (assuming this is a control). However, if you do not have thousands of fonts generated during your application life cycle, you no longer need to delete the font at all. If you have only 5-10 fonts in the application, removing them should not cause much concern.

+4
source

Source: https://habr.com/ru/post/978372/


All Articles