in Education by
What is the diference between those two examples of code? public Test(int x) { List list= new List(); List list1 = new List(); list= CreateList(x); list1 = CreateList(x + 1); DoStuff(list, list1); list.Clear(); list = null; list1.Clear(); list1 = null; } Is this the way to code? public Test(int nCount) { var list = CreateList(nCount); var list1 = CreateList(nCount + 1); DoStuff(list, list1); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You're not actually comparing like with like as all var means is "let the compiler decide what type this is". It will compile to the same code (assuming that CreateList returns a List). So while your code is functionally identically you're doing extra work in the first example that you're not replicating in the second. In the first case you are creating lists unnecessarily as the act of returning a list from CreateList(x) will overwrite what you have just created. A more accurate question would be asking for this difference between: public Test(int x) { List list= CreateList(x); List list1 = CreateList(x + 1); DoStuff(list, list1); list.Clear(); list = null; list1.Clear(); list1 = null; } and public Test(int nCount) { var list = CreateList(nCount); var list1 = CreateList(nCount + 1); DoStuff(list, list1); } In both cases as the lists fall out of scope they'll be marked as eligible for garbage collection. You don't need the calls to Clear() or to set the variables to null.

Related questions

0 votes
    In my Silverlight application I want to show a child window which prompts the user if he want to leave ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Predict the output of the following JavaScript code.
    I have a number of user permissions that are tested throughout my ASP.NET application. These permission ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    We have some .NET code that does a call out to a COM component (dnsapi.dll) to query whether a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
0 votes
    What Will Happen When Power Factor Is Leading In Distribution Of Power?...
asked Dec 12, 2020 in Technology by JackTerrance
0 votes
    Lets say I have the option of identifying a code path to take on the basis of a string comparison ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Lets say I have the option of identifying a code path to take on the basis of a string comparison ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to write generic method to cast types. I want write something like Cast.To(variable) instead ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I'm making a drag and drop game for my A level computing coursework. My drag and drop works fine, but I have 6 ... >= 190 && theButton.Location.X = 42 && theButton.Location.Y...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    When will the finally block be called? (a) When there is no exception (b) When the catch does ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Code A: var x = 10; y = --x + 1; alert(y); Code B: var x = 10; y = x-- + 1; alert(y); What is the output for code A and B? 1. 10,11 2. 10,10 3. 11,10 4. 11,11...
asked Feb 23, 2021 in Technology by JackTerrance
...