Given the following code, how would you refactor it to use LINQ
public float GetTaxAmount(float taxPercentage, float[] orderItemAmounts)
{
float total=0;
for(int index=0; index <orderItemAmount.Length; index++)
{
total +=orderItemAmounts[index]* taxPercentage;
}
return total;
}
1. Debug your code, you will know the sequences.
2. Im not sure what do you want to do... you can't use catch(MyExcepton) in the code..you have already catch all exceptions.
3. ...please try it by yourself.
4. Use Sum() to get total number * taxPercentage.
Best Regards.
Molly
It's time to start living the life you are imagined.
the first one, I tried to debug , Dispose() always called before Execute() Returns, because it is in the end of Execute Function.
class Datasource: IDisposable
{
public void Dispose(){/*clean up*/}
}
class MyClass
{
void Execute ()
{
var ds =new DataSource();
ds.DoStuff();
ds.Dispose();
}
}
Here is my test code for a console application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var ds =new DataSource();
ds.Dispose();
}
}
class DataSource : IDisposable
{
public void Dispose(){/*clean up*/}
}
}
From your testing code, you didn't have excute(), I think Dispose() is a function in Database Class, unless you call it by ds.Dispose(), it can't execute automatically.
Best Regards.
Molly
It's time to start living the life you are imagined.
super123
Member
82 Points
181 Posts
questions
Oct 26, 2012 08:29 PM|LINK
1.
what can be added to the following code snippet to ensure the Dispose method is always called before the Execute method returns?
class Datasource: IDisposable
{
public void Dispose(){/*clean up*/}
}
class MyClass
{
void Execute ()
{
var ds =new DataSource();
ds.DoStuff();
ds.Dispose();
}
}
2. What will the console output be when DoExceptionalStuff() is executed, and why?
class MyClass { void DoExceptionalStuff() { try { throw new MyException(); } catch (Exception ex) { Console.WriteLine("ex!"); } catch (MyExcepton mex) { Console.WriteLine("mex!"); } } } class MyException :Exception{}3.
Would a service implementing this interface initialize properly? if not, what needs to change for this to work?
[ServiceContract] interface IEchoService { [OperationContract] string Echo(string someString); [OperationContract] int Echo(int someInt); }4.
Given the following code, how would you refactor it to use LINQ public float GetTaxAmount(float taxPercentage, float[] orderItemAmounts) { float total=0; for(int index=0; index <orderItemAmount.Length; index++) { total +=orderItemAmounts[index]* taxPercentage; } return total; }dotnetdev201...
Member
321 Points
80 Posts
Re: questions
Oct 26, 2012 08:37 PM|LINK
Have you actually tried to run any of this and answer your own questions?
This forum is here to help people, not to answer your textbook questions for class. Take some initiative and try it yourself first.
molly_c
Participant
1590 Points
401 Posts
Re: questions
Nov 02, 2012 04:49 AM|LINK
1. Debug your code, you will know the sequences.
2. Im not sure what do you want to do... you can't use catch(MyExcepton) in the code..you have already catch all exceptions.
3. ...please try it by yourself.
4. Use Sum() to get total number * taxPercentage.
Molly
It's time to start living the life you are imagined.
super123
Member
82 Points
181 Posts
Re: questions
Nov 07, 2012 06:21 PM|LINK
Hey there,
the first one, I tried to debug , Dispose() always called before Execute() Returns, because it is in the end of Execute Function.
class Datasource: IDisposable { public void Dispose(){/*clean up*/} } class MyClass { void Execute () { var ds =new DataSource(); ds.DoStuff(); ds.Dispose(); } }Here is my test code for a console application :
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var ds =new DataSource(); ds.Dispose(); } } class DataSource : IDisposable { public void Dispose(){/*clean up*/} } }Thanks,T
molly_c
Participant
1590 Points
401 Posts
Re: questions
Nov 09, 2012 01:48 AM|LINK
I don't get your point..
From your testing code, you didn't have excute(), I think Dispose() is a function in Database Class, unless you call it by ds.Dispose(), it can't execute automatically.
Molly
It's time to start living the life you are imagined.