Can a Finalizer use a string member? [duplicate] - c#

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.

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.

What is difference in defining properties in these two methods in c#? [duplicate]

This question already has answers here:
What's the difference between encapsulating a private member as a property and defining a property without a private member?
(5 answers)
Closed 6 years ago.
I came across the question in interview, defining the property in Class. I know this is very basic question but would like to get discuss with experts here.
Method 1:
public string MyProperty { get; set; }
Method 2:
private string _myProperty;
public string MyProperty
{
get
{
return _myProperty;
}
set
{
_myProperty = value;
}
}
Which will better in performance in terms of extra variable need to declare in Method2.
Both methods use the same number of variables, in the first version the extra variable is just hidden by the compiler, if you decompiled the code you would see it too uses a variable.
Method 1 is a "newer" way of doing things. It was introduced in C# 3.0. Behind the scenes it is in fact using a private backing variable... it's just neater.
https://msdn.microsoft.com/en-us/library/bb384054.aspx
Method 2 would probably perform "better" only in the cases where you needed to directly access that variable from within the class itself. But performance wise between the two, it's probably very negligible. You wouldn't need to go through the setter/getter.
However, method 1, in my opinion, just provides a better flow of control, keeps code cleaner, and ensures you're always going through a setter/getter and not directly accessing the private variable itself.
Method one is the quick way to implement it and if no extra checks have to be done then that can be used. However, option two is used when you want to sanitize inputs to the class, use security or just want to make sure that he user has to enter something valid as all of your checks can be done in the function before setting the private varible which can be easily canceled in the code.

Does it make any practical difference to instantiate an object inside the constructor? [duplicate]

This question already has answers here:
What is the difference between instantiation in constructor or in field definition?
(5 answers)
Initialize class fields in constructor or at declaration?
(16 answers)
Closed 8 years ago.
Is there any practical difference between those two ways of instantiating an object?
public class myClass
{
private myType myObject = new myType();
}
and
public class myClass
{
private myType myObject;
public myClass()
{
myObject = new myType();
}
}
Thanks for helping.
No there isn't any practical difference, between the two ways you provided. They are exaclty the same.
The answer is yes
When you read the code later, you will look in the constructor to see what happens when you create the class. If you put constructor logic outside the constructor another developer may miss what's going on. So put your constructor logic in your constructor. It makes a difference.
As far as the code goes, there isn't much difference. Initialization in the declaration happens in document order, top to bottom, which might or might not have side effects. From a practical perspective, if you do any interactive debugging at all, you'll grow to hate declaration initializations, unless you enjoy stepping through them one at a time.
Keep your code tidy and initialize things in the constructors.

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.

Categories

Resources