|
Quote: I am still eager to here how you use anonymous classes
In the aforementioned case, it would be impossible
The rest of the possible cases is indeed described by Jon Skeet, and while he calls them a 'hack', any of those ways should be preferred over tuples (IMO).
|
|
|
|
|
Leppie wrote[^] re: the discussion of the use of Tuples (on the Lounge): "Anonymous classes handle most of these cases and cleaner in my opinion.
Who knows what Item0 and Item1 and Item2 is?" .Of course Leppie is absolutely right: Tuple elements cannot be accessed by index; they cannot be foreach iterated, and, the following "legal" way to find out, at run-time, what Type an Item1 ... Item#n is, for example: "_fourTuple.Item1.GetType();" is something that would make me gag if I saw it in "real code."
The main reason I am writing this is to ask Leppie to please reply with an example of the use of anonymous classes, which interests me very much, and I think will add value to this discussion.
thanks, Bill
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut
|
|
|
|
|
I agree in general but you know, statically, what the type of the second item of a Tuple is. If the Tuple is Tuple<T, U> then the type of the second item is U.
|
|
|
|
|
Hello,
In the following function, I am trying to return two (2) values instead of one. Can someone please say how this can be done? I was thinking possibly returning an array from the function, but I do not know if this is possible. Does anyone have an idea of how multiple values can be obtain from the same function?
Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testing_Functions
{
class Program
{
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return Test_Val;
}
static void Main(string[] args)
{
Double A = 1, B = 2, C = 3;
Double Result = Test(A,B,C);
}
}
}
|
|
|
|
|
There are a few ways of obtaining multiple values.
1. Create an struct/class that holds more than one value
2. Use an array or list (the same as 1 really)
3. Use an out (or ref if needed) parameter.
1:
public class Pair<T>
{
private T first;
private T second;
public Pair(T first, T second)
{
this.first = first;
this.second = second;
}
public T First { get { return first; } }
public T Second { get { return second; } }
}
public static Double Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
return new Pair<Double>(Test_Val, Second_Test);
}
2 should be straight forward.
3:
public static Double Test(Double Val1, Double Val2, Double Val3, out Double secondResult)
{
Double Test_Val = Val1 + Val2 + Val3;
secondResult = Test_Val % 2;
return Test_Val;
}
|
|
|
|
|
out and ref parameters are usually considered to be dirty, so stay away from that if you can.
Depending on what the function does, you may want to create a wrapper class or struct for the result. That means that you get semantically useful names, instead of First and Second from the Pair class.
|
|
|
|
|
Also, he could return a Tuple<type,type> but by far the best is solution 1.
|
|
|
|
|
Use ref and out keyword to return multiple values from function
return statement returns only single value.
|
|
|
|
|
the quick and dirty way:
public static Double[] Test(Double Val1, Double Val2, Double Val3)
{
Double Test_Val = Val1 + Val2 + Val3;
Double Second_Test = Test_Val % 2;
double [] arr = { Test_Val, Second_Test };
return arr;
}
Just to show you, you can return virtually anything, including Lists, Dictionaries, self made object (like the Pair example)
V.
|
|
|
|
|
|
Please am working on an application that use datagrid view but am looking for tutorial or code that i can use to insert,update record that i retrieved from the database.
|
|
|
|
|
|
Don't use short URL's. People have no idea where that URL is actually going.
|
|
|
|
|
|
I wanna know, is it possible to read windows 7 User account password from the windows registry?
|
|
|
|
|
A Reset Tool downloaded here[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I want to know can we write a piece of code in c# that gets it from registry or not?
even is the windows password saved in registry or not?
|
|
|
|
|
No.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
|
Kuthuparakkal wrote: Since a hash function is one-way, this provides some measure of security for the storage of the passwords.
..which means you can read the hash, but not the password.
Kuthuparakkal wrote: Boot with some instant (DVD) os and read it.
..and end up with useless data; it's not possible to get the key, decrypt the hash and find the matching password. You could wipe out the entire section with a tool, but that's about it.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
|
No hacker has the required resources.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
No, there are people out there who know how to get the hashes. After that, you have to brute-force dictionary attack the hashes to get the passwords which can take a considerable amount of time.
|
|
|
|
|
Not yet, as far as I know. At least, not if the password isn't short (or findable by a dictionary attack).
On XP and previous, it was trivial to crack that nice LM hash, but Vista+ doesn't store the LM hash anymore.
|
|
|
|
|
<pre lang="c#"></pre>Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication_FileTest.Program.GetAverageSalary(string)' C:\Users\Dawn\documents\visual studio 11\Projects\ConsoleApplication_FileTest\ConsoleApplication_FileTest\Program.cs 98 24 ConsoleApplication_FileTest
<pre lang="c#"><pre>static void Main(string[] args)
{
Dictionary<string, double> averages = new Dictionary<string, double>();
string filename = "c:\\test.txt";
averages = GetAverageSalary(filename)
} </pre></pre>
=====================
<pre lang="c#"> Dictionary<string, double> GetAverageSalary(string filename)
{
... </pre>
|
|
|
|