Difference between Strong Reference and Weak Reference in .NET Projects

Posted by in C# category on for Beginner level | Points: 250 | Views : 12049 red flag

In this article we will discuss about the differences between strong reference and weak reference in .NET Projects and will also learn about strong name key or public key token.

Strong Reference and Weak Reference

.NET fundamentals is an ocean in itself there are many essential topics that are must learning while some are of worth knowing it. Here today we will see something from GAC(Global Assembly Cache). When we talk of GAC then it means we are dealing with assembly and DLL(Dynamic Library Link). In this article we will see differences between strong reference and weak reference also we will learn about strong name key or public key token. We will see practical’s of strong reference and weak reference to get clear understanding of the topic.

Weak Reference

For that below is a simple project where we have class library(DLL) and a windows client(Windows Application) . Under a class library we have a simple call back method named “CallMeBack()” and this class library is consumed by windows client with an event action when “button” is clicked by displaying message present under CallMeBack() method.

In order to consume ClassLibrary we need to add it as “reference” in Windows Application then add it’s namespace with syntax “using ClassLibrary1” under Form1.cs of Windows Application. Thereafter create object named “cllib” of “Class1” present in “ClassLibrary1” so that method “CallMeBack” can invoked which will display text “I am the actual class” on message box when button clicked.

Now we will look into weak reference and strong reference for that first copy existing project code from project folder to new location, here location used in “D:\CSharp\StrongWeakRef”

After you copy and paste “ClassLibrary.dll” and “WindowsFormApplication1.exe” just do double click and run “WindowsFormApplication1.exe” file and check whether you see correct output which is by displaying message “I am the actual class” on message box as shown in the figure down below.

As output is seen clearly now if we ship our files to any environment and also if you do the deployment of it will work perfectly fine.

Now suppose if tomorrow some known person or internal developer if tries to change or alter your .DLL file from actual one and replaces it with his file, then that can be easily achieved by him. As knowing inner details of “ClassLibrary1.dll”like function or method name and also data types used in .DLL file can be done by ILDASM tool.

This is big issue without your concern the actual .DLL is replaced by someone code which is written by himself. This is because our client(windows application) is not tied up with .DLL(Class Library) file due to which client has no idea with which .DLL file it is working? Is .DLL published by the original publisher or not. In short it can be easily manipulated because it only knows the name of .DLL and within it is only concern with namespace, class, function or method name and if found correct those things it works fine with it.

This is termed as Weak reference where client is identifying .DLL and assembly by it given name and internally with namespace, class and function or method names only.

So now question is how to prevent this from happening?

This can be done by assigning Strong Name to the .DLL or assembly which is called as Strong reference. With use of Strong Name client will be able to identify original publisher and issue of weak reference will be resolved. Now client will identify .DLL or assembly with strong name and other internal details where strong name is unique across all and same cannot be created by anyone.

Strong Reference

How to assign Strong Name to .DLL practically?

  1. For that go on Visual Studio under Solution Explorer do a right click on “ClassLibrary1”.
  2. Then click on Properties.

Once the Properties screen is open on that do the following: -

  1. Click “Signing” option available on the left of Properties window.
  2. Do a check mark on the tick option of “Sign the Assembly”.
  3. From the dropdown select “<New...>” which will open “Create Strong Name Key” window.
  4. Under new window give suitable name to key “MyStrongName”.
  5. Do uncheck to create key file without password.
  6. Click OK to close the “Create Strong Name Key” window.

Once clicked OK it will create a file with name “MyStrongName.snk” which will be associated with .DLL. Now there will be a strong bonding between client and .DLL file using this key file.

Now rebuild the solution again and then copy "ClassLibrary1.dll" and "WindowsFormsApplication1" file from existing project location to new folder location “StrongWeakRef”.

Run .EXE file and see whether new output is seen with “I am the actual strong class” message displayed on the message box.

Now enable altered .DLL which is named to “ClassLibrary1altered.dll” to “ClassLibrary1.dll” and then run .EXE client "WindowsFormsApplication1". It will open “Form1” and then click “button1” to see output it will throw error. If you read error message which states that client is not able to match up strong name key of altered .DLL. “MyStrongName.snk” is strong name key of strong .DLLassociated with client "WindowsFormsApplication1".

With the above demonstrated practical hope that difference between weak reference and strong reference along with strong name key is clearly understood by the reader.

Also see an interesting article which is referred to topic strong reference called delay signing.

Following is the video from below fresher’s C# project series which will be helpful to the one who is new to C# programming language: -

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Ahtesham Shaikh
Member Level: Bronze
Member Status: Member
Member Since: 10/3/2016 2:36:04 AM
Country: India
Ahtesham Shaikh
Hey Friends, Myself Ahtesham a coder, writer & technical supporter for IT world and for my company Questpond. Having 10+ years of experience in microsoft technologies like WCF, WPF, MVC, Sharepoint, AngularJS, MSBI, Excel 2000 - 2013, Powerpoint, C#, SQL Server and so on. I just find myself happy and satisfy by doing this job. Apart from profession love reading novels and other digital network books. I'm a faculty at Questpond and do take trainings on c#, object oriented programming, msbi, sharepoint, mvc, mvc, angularJS and so on. Feel free get in touch me for one-to-one trainings, offline trainings @Andheri Mumbai, Online training via microsoft live meeting.

Login to vote for this post.

Comments or Responses

Posted by: Aprilrussell on: 9/26/2017 | Points: 25
Gr?at article, totally what I wanted to find. Thanks for sharing!
Survival games: http://survivalgames.me
Posted by: Aptroninnoida on: 7/22/2019 | Points: 25
In practice “reference” normally means “strong reference”.

However sometimes it means “either a weak or a strong reference”.

A strong reference in a garbage collected world means “I point to that thing over there, and it is not ok to remove that thing unless you can also remove me”. In a reference counted world it is just the pointer with rules for incrementing/decrementing a reference count that does basically the same thing you get in the GC world except if you have a cycle you end up with memory that you can’t figure out is unused so you leave it allocated forever.

A weak reference is “I point to that thing over there but make no claims or promises that my existence will keep that thing alive”. A zeroing weak reference says that plus “and I will become nil so you can tell the thing I point to is dead when that thing does”. A nonzeroing weak reference is a good way to shoot yourself in the foot.

Weak references are frequently used either to implement caches or to workaround retain cycles in a non garbage collected world (they are the price you pay for more predictable performance and destruction times).

Login to post response

Comment using Facebook(Author doesn't get notification)