Member Avatar for Member #957165

this might be a silly question :-) but just for reference asking that
System.Drawing.Graphics is a reference type ? or value type ?
I mean when creating objects like this

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim i As Integer = 10
        Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
        g.DrawString(i, New Font("Arial", 20.0F), Brushes.Blue, New PointF(0, 0))

    End Sub

is g a reference type varable ?
I HOPE IT IS YES ?

Dani AI

Generated

's short answer was right: System.Drawing.Graphics is a managed class (a reference type) rather than a ValueType. The practical consequences matter more than the label — Graphics instances represent a GDI+ drawing surface and are obtained (not constructed directly) through factory methods or by the framework during paint. (learn.microsoft.com)

There are a few common ways a Graphics instance appears in code: the framework supplies one in OnPaint via PaintEventArgs.Graphics (preferred for on-screen drawing); or a Graphics can be created with methods such as CreateGraphics, FromImage, FromHwnd/FromHdc when drawing to a bitmap or native device context. For standard control painting the recommended pattern is to draw inside OnPaint (use the supplied Graphics) rather than repeatedly calling CreateGraphics/FromHwnd. (learn.microsoft.com)

Ownership and disposal rules are the thing that trips people up. Graphics implements IDisposable, so any Graphics created explicitly by user code (FromImage, CreateGraphics, FromHwnd, etc.) must be disposed (using a using block or explicit Dispose) to avoid leaking OS/GDI resources. The Graphics instance handed in via PaintEventArgs is owned by the framework and should not be disposed by caller code. Likewise, disposable helpers created for drawing (Pens, Brushes, Fonts) should be disposed when created; by contrast, shared system Pens/Brushes (Pens/SystemPens, Brushes/SystemBrushes) are cached and must not be disposed. (stackoverflow.com)

A few practical notes tied to the posts above: prefer the PaintEventArgs.Graphics in OnPaint over Graphics.FromHwnd in paint handlers; enable double buffering or ControlStyles.OptimizedDoubleBuffer to reduce flicker; and for modern, cross-platform projects note that System.Drawing.Common became Windows‑specific on .NET 6 (use SkiaSharp, ImageSharp, etc. for cross‑platform work). These are the maintenance and portability issues that matter long after the simple “value vs reference” question is answered. (learn.microsoft.com)

Recommended Answers

All 5 Replies

Simple types such as Integer, Boolean, etc (i.e. types that do not require instantiation with "New") are value types. Anything that is used to access an object is a reference type. In your example, Graphics refers to an object (as indicated by the fact that you can access methods/properties via dot notation).

Short answer - YES.

Member Avatar for Member #957165

Thanks dude, the ansewer is short but very clear ,it's just I had a small doubt about this one ....

As a point of interest, the designers of VB could have made ALL variables objects (reference types) but there is a performance price to pay and it was decided to leave the simple type as simple types to avoid this penalty.

As a point of interest, the designers of VB could have made ALL variables objects (reference types) but there is a performance price to pay and it was decided to leave the simple type as simple types to avoid this penalty.

???
From the documentation for the ValueType Class:

ValueType overrides the virtual methods from Object with more appropriate implementations for value types. See also Enum, which inherits from ValueType.

Data types are separated into value types and reference types. Value types are either stack-allocated or allocated inline in a structure. Reference types are heap-allocated. Both reference and value types are derived from the ultimate base class Object. In cases where it is necessary for a value type to behave like an object, a wrapper that makes the value type look like a reference object is allocated on the heap, and the value type's value is copied into it. The wrapper is marked so the system knows that it contains a value type. This process is known as boxing, and the reverse process is known as unboxing. Boxing and unboxing allow any type to be treated as an object.

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

You can pass a value such as variable,char,boolean in the function,it is referred as value type and its return a value.If you can pass a reference such as array,structure,string,pointer in the function argument,it is referred as reference type and it return a reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.