C# override in Finalize() [duplicate] - c#

This question already has answers here:
In C# how to override the Finalize() method?
(3 answers)
Closed 9 years ago.
protected override void Finalize()
{
try
{
this.FtcpSock.Close();
this.FudpSock6800.Close();
this.FudpSock6801.Close();
this.FudpSock6802.Close();
this.FudpSock6803.Close();
this.FudpSock6804.Close();
this.FudpSock6806.Close();
}
finally
{
this.Finalize();
}
}
I m getting this Error Message:
Error 1 Do not override object.Finalize. Instead, provide a destructor.
by the way, this is Original Code complied by third party company.
how to solve this problem? how to Finalize with override?

As the message states, you shouldn't override Finalize directly but instead provide a destructor. Ignoring that though the code has one other fatal problem
protected override void Finalize() {
try {
...
} finally {
this.Finalize();
}
}
This code will eventually lead to a stack overflow in the code. It is recursively calling itself in a finally block and hence will just loop until you run out of stack space. The code was likely meant to use the following instead
base.Finalize();
This will chain to the implementation of Finalize on the base type

If you can edit the code, you can move the code to a destructor. For a form called 'Form1' the destructor would look like:
~Form1()
{
this.FtcpSock.Close();
this.FudpSock6800.Close();
this.FudpSock6801.Close();
this.FudpSock6802.Close();
this.FudpSock6803.Close();
this.FudpSock6804.Close();
this.FudpSock6806.Close();
}
http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

Related

How to call Destructor [duplicate]

This question already has answers here:
Manually destroy C# objects
(8 answers)
Closed 6 years ago.
I know destructor are called by Garbage Collector when object is no longer used.
But I want to know
How to call destructor through c# code?
If possible please give some basic example for understanding.
You don't call the destructor in .NET
The managed heap is handled by the CLR and the CLR only.
You can however define a destructor to a class, the destructor would be called once the object gets collected by the GC
class Foo
{
public Foo()
{
Console.WriteLine("Constructed");
}
~Foo()
{
Console.WriteLine("Destructed");
}
}
Take notice that the destructor doesn't (and can't) have a public modifier in-front of it, it's sort of an hint that you can't explicitly call the destructor of an object.
You can look at the Destructor Microsoft docs.
You need to declare a function with same name as the class name but with a leading "~" sign.

Can someone please explain what is protected override void? [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 6 years ago.
I am learning about xamarin in basic stage. All the function calls are done by 'protected override void'. So that anyone please help what is the use of it. How it differs from public void.
protected - access modifier (in C# you have public, protected, private, internal)
override - you override virtual method which was implemented scope above
void - return type of your method
public - everyone can access this method.
protected - only inherited classes can access this method.

Can Extension-Methods overwrite instance methods? [duplicate]

This question already has answers here:
Is there any way in C# to override a class method with an extension method?
(4 answers)
Closed 8 years ago.
Is it possible to overwrite instance methods by an extension-methods? For example, I have an assembly (compiled, without sources) and I want to rewrite some behaviour.
I created some code for test:
public class SomeClass
{
public void MyMethod()
{
Console.WriteLine("Instance method is called");
}
}
public static class ExtMethods
{
public static void MyMethod(this SomeClass c)
{
Console.WriteLine("Extention method is called");
}
}
then I try to use:
SomeClass s = new SomeClass();
s.MyMethod();
It's compiled successfully, IntelliSence marks this method as instance (extension method has another icon)
output says
Instance method is called
none note about extention method exists. So, no way to overwrite instance method, right?
Why the behavior of the compiler and VS is such non-informative? If I develop an extension-method and there exists already the same instance method (and I don't know about it), I can spend hours to understand why behavior is different than expected...
No, you cannot override the instance method with extension methods. Hence, the name 'extension' method, you can only extend the instance methods.
An instance method will always override the extension method.
If you want to force to call the extension method, call it as a regular static method:
ExtMethods.MyMethod(yourClassInstance);
About the why VS doesn't tell you it is a duplicate: actually it can't do that. What if you have an extension method on object. Should VS check all classes and their methods if any method is a duplicate? They simply didn't build the check, it is something you should do yourself.

What is tilda character in c# constructor? [duplicate]

This question already has answers here:
What does the tilde before a function name mean in C#?
(6 answers)
Closed 9 years ago.
In doing a code review I came across this:
public class ClassTilda {
~ClassTilda(){
//code
}
}
Why is this building and what is that tilda? Can you provide a reference?
This is Destructor, which is basically not suggested to use in C#.
Destructors are used to destruct instances of classes.
Like was mantioned in comments: there are still cases when you would like to manage, but in most cases it's avoidable as you have:
IDisposable when dispose is called on instance of your object interface
Finalize() when GC is going to clean your type, so called by GC itself.

Can a Finalizer use a string member? [duplicate]

This question already has answers here:
Which objects can I use in a finalizer method?
(3 answers)
Closed 8 years ago.
I have code like this:
~MyClass() {
try {
if (Database.Exists(_connectionString))
{
Database.Delete(_connectionString);
}
} catch { }
}
Database is a static class of Entity Framework, whereas _connectionString is a private readonly string set by the ctor. The idea is that if someone forgot to Dispose the class, we still clean state (in my case, this is part of an integration test where the test runner doesn't call Dispose if there's an unhandled exception in the test, so it's not something I can fix on my side)
However, Finalizers are generally not supposed to call class members because they might be disposed already, so if I end up in a scenario where the _connectionString is already collected, I might have a problem.
Is there a way to do this safely (e.g,, using some sort of GC.KeepAlive construct?)
Edit
As pointed out, below was not a direct answer to the question so....
Yes it is safe to access the member variables (in this particular example - assuming the connection string is a string) and in addition I recommend you check out the IDisposable pattern.
Original
You should implement the IDisposable pattern:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
This will ensure that the Dispose method is always called and you can safely access the member variables.

Categories

Resources