|
Thanks, Leppie, for "weighing in here:" your comment on anonymous classes has lead me to some interesting technical geography.
Of course, I am still eager to here how you use anonymous classes
I've been reading, on various web-sources, mainly StackOverFlow, discussions of anonymous classes: the problem with SO is that while the OP may mention the words "anonymous classes," the discussion soon turns, it seems, to anonymous types, and use of 'dynamic, and 'var.
This SO thread was interesting for the sheer volume of responses and debates:"A generic list of anonymous class"[^].
Probably the two most interesting (and exotic) discussions I found were by Jon Skeet: "Horrible grotty hack: returning an anonymous type instance"[^], and on a site called DevCurry: "Look Ma! No Classes – Creating Generic List(T) collection of Anonymous Types and calling Extension Methods"[^].
In the comments section of the Skeet article is this wonderful exchange between Jon and one "Erik:"
# re: Horrible grotty hack: returning an anonymous type instance
"This is one of those examples where relying on internal implementations can bite you, I imagine - since anonymous types are by design not supposed to leave private scope, the compiler team is free to do whatever they want with the actual implementation.
Thanks for the brain-twitch, though, Jon. =)
Friday, January 09, 2009 12:21 PM by Erik"
# re: Horrible grotty hack: returning an anonymous type instance
"@Erik: No, it's not implementation-dependent. The compiler-generated name is, but the language spec guarantees that two anonymous object creation expressions using the same property names and compile-time types within the same assembly will create instances of the same type. See section 7.5.10.6 of the C# 3.0 spec.
Friday, January 09, 2009 1:48 PM by skeet"
best, 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
|
|
|
|
|
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.
|
|
|
|
|