|
Hi Somebody has asked me a question in interview.
we have two classes Class1 and Class2
public class Class1
{
private Class2 obj;
public void method1()
{
obj = new Class2();
}
public void method2()
{
obj.method1();
}
}
public class Class2
{
public Class2()
{
GC.SuppressFinalize(this); }
public void method1()
{
}
}
Now in GUI form: onbutton_Click I write the follwing code:
Class1 class1 = new Class1();
class1.method1();
class1.method2();
After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Ashwani
|
|
|
|
|
Ashwani,
Of course the easy answer is YES...the garbage collector will eventually get everything sooner or later(even if it has to wait for a reboot ). The better question, and the one they probably intended was, "Are the objects of Class1 and Class2 marked for GC or not?". The answer is still yes.
I assume the bold on the GC.SuppressFinalize(this); line is primarily what they are asking about. That line HAS NO MEANING WHATSOEVER IN THIS CONTEXT. That line is meant to short circuit the running of a destructor/finalizer for a class. Since neither of the classes has a destructor defined, the line of code does nothing.
In this case both classes will be marked for GC at the end of the click event code.
HTH
WhiteWizard(aka Gandalf)
|
|
|
|
|
Thanks for your reply.
This is what I have answered in the interview. But the interviewer was not agree with me.
Can I see it visually(When these objects actually collected by GC) using any tool provided by Microsoft or by any other company.
Ashwani
|
|
|
|
|
Someone could hate interviews, especially when the interviewers are not programmers
SkyWalker
|
|
|
|
|
Ashwani_kumar wrote: This is what I have answered in the interview. But the interviewer was not agree with me.
Then the interviewer is an idiot.
If the interviewer is going to ask question they must do adequate research that they, at the very least, don't look like a complete fool.
If they offer you the job, think very carefully about whether you actually want to work there or not. Some people get very defensive if you tell them they are wrong. Remember: The mind is like a parachute; it works best when it is open.
|
|
|
|
|
Colin Angus Mackay wrote: The mind is like a parachute; it works best when it is open.
I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Dave Kreskowiak wrote: Colin Angus Mackay wrote:
The mind is like a parachute; it works best when it is open.
I don't know about that. I've seen some minds that are so far open that air just passes straight through them!
Don't be so open minded your brain falls out.
--
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
dan neely wrote: Don't be so open minded your brain falls out.
It's way, way, way too late for most of those people! Their brains have long since been washed down the storm drains...
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Ashwani_kumar wrote: After the above code is finished, WILL Garabage collector collect objects of Class1 and Class2 or not?
Yes. It will garbage collect instances of class1 and class2.
|
|
|
|
|
Yes, it will. SuppressFinalize just tells the GC to remove the object from the finalization queue. It still gets collected.
The interviewer told you you were wrong?? Doesn't sound like a company I'd want to work for then...
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi i working at the moment with serialization and i have a simple question.
When is it necessary to implement the ISerializable Interface and when is it enought to mark an klasse as [Serializable] ?
|
|
|
|
|
Marking a class as [Serializable] means all public members will be autoomatically serialized and deserialized. Uisng the ISerializable interface allows you fine control over this process.
|
|
|
|
|
What do you mean with fine control ?
Do you mean it so,that i have to implement the [ISerializeable] interface then when my class have private members ?
|
|
|
|
|
I don't know where J4amieC got this from, but adding [Serializable()] to a class works perfect for private as well as public members. To exclude a member from serialization you can use [field:NonSerialized()] , when you want to upgrade a class with additional members that require to be serialized and maintain backward compatibility with the previous version of your class you can use [field:OptionalField()] on the member. Frankly, I don't ever use the ISerializable interface.
Standards are great! Everybody should have one!
|
|
|
|
|
When i only mark a class as [Serializeable] then i got an SerializeException when i deserialize the class wich say that the necessary constuctor of the class is missing.
So thats why i implemented the ISerializeable-Interface with its methodes...
[SecurityPermissionAttribute( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )]
void ISerializable.GetObjectData( SerializationInfo info, StreamingContext context ) {
this.GetObjectData( info, context );
}
public override void GetObjectData( SerializationInfo info, StreamingContext context ){
info.AddValue( "member", member );
...
}
...and the necessary constructor...
public ClassName( SerializationInfo info, StreamingContext context ){
member = info.GetValue( "member", typeof( member ) );
...
}
and cause that works fine was i thinking...
"Everytime when i wanna serialize a class i have to implement the ISerializeable-interface"
...but i read somewhere that it is enough in some cases to mark a class as [Serializeable].
BTW: i use BinaryFormatter to serialize and deserialize my classes.
|
|
|
|
|
Marking your class [Serializable()] is only enough if all of your class members are themselves serializable: The exception you're getting is because one of your members doesn't support serialization.
If you don't really need the member just mark it as [field:NonSerialized()] and the exception will go away.
Otherwise, make sure it supports serialization. You can do this by marking the class as [Serializable()] itself. Determine which members should be saved, mark all others as [field:NonSerialized()], repeat the operation for your member's memberclasses (and enumerations), and so on, until you reach a class with only primitive types.
This mechanism provides an easier, faster and less errorprown way to serialize, I prefer it over ISerialzable...
Standards are great! Everybody should have one!
|
|
|
|
|
Can a ToolStripProgressBar/progressbar take a float value to get incremented? If so then how?
|
|
|
|
|
GunaChinna wrote: Can a ToolStripProgressBar/progressbar take a float value to get incremented?
No, it can't.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
i need an event in datagridview that will be called for every row that is binded to the grid in windows application. is there an event?
|
|
|
|
|
Why do you need the event? there is always an option to access the data that is binded to the datagrid. If you populate your grid with a dataset try
<code>
foreach (System.Data.DataRow dr in dsModApp.Tables[0].Rows)
{
myList.Add(dr.ItemArray[0].ToString());
}
</code>
here i retireve the first column that is my select statement and thus position [0] in the dataset that is returned. Then i add the information to an arraylist for other purposes. hope it helps
Riaan Booyzen
|
|
|
|
|
Hi,
You can use the RowsAdded event on the grid. It is fired when you add a row to the grid... It includes when you bind a dataset, then it is fired for every row.
|
|
|
|
|
how to run console application in command prompt.
the keywords to run...
like csc Program.cs...........
whats it i dont know exactly how to do it.
|
|
|
|
|
After you have compiled the application, you will find the .exe file in the bin folder. That is the program that you run.
---
Year happy = new Year(2007);
|
|
|
|
|
csc compiles the cs file. The exe will be generated. You then run the exe.
|
|
|
|
|
You know there is a Main method like this;
static void Main(string[] args)
args is an array which contains commands of your program.exe
check this one;
static void Main(string[] args)<br />
{<br />
if (args[0] == "hello")<br />
{<br />
Console.WriteLine("args[0] = {0}", args[0]);<br />
}<br />
if (args[1] == "world")<br />
{<br />
Console.WriteLine("args[1] = {0}", args[1]);<br />
}<br />
}
in this case, write "programpath\yourprogramname.exe hello world" and viola!
|
|
|
|