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

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.

Related

C# method that determines if given object is a singleton instance [duplicate]

This question already has answers here:
C# .Equals(), .ReferenceEquals() and == operator
(7 answers)
Closed 7 months ago.
I need to complete the piece of code to write a method that takes a factory method that returns an object and determines if the object is a singleton instance.This is for an exercise of a course i am taking about design patterns. All the previous exercises were about implementing the pattern but this one is about testing and i dont know how to test this. I tried to create another instance and check if the hashcodes are the same but i dont know how to create another instance with that function.
public class SingletonTester
{
public static bool IsSingleton(Func<object> func)
{
// todo
}
}
Singleton is a semantic definition, it is not generally testable through code. No interface can guarantee that the class operates as a singleton.
Because of the semantic nature it would be better to create a custom annotation to indicate if the object is a singleton.

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.

Implement an interface while calling a method (inline) [duplicate]

This question already has answers here:
Can anonymous class implement interface?
(9 answers)
Closed 6 years ago.
In Java, assuming that class A is an interface/abstract class, I can do the following:
callMethod(new A(){
public void myFunc(){
System.out.println("test");
}
});
How can I achieve the same shortcut effect in C# without having to declare a class seperately .
Thanks
You can't.
C# does not allow you to implement interfaces on Anonymous Types.

Why members in static class are not static by default [duplicate]

This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static 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