I'm new to C# and used to Swift, so please bear with me.
I would like to use types in C# exactly in the manner described by the Swift code below:
typealias Answer = (title: String, isCorrect: Bool)
typealias Question = (Question: String, Answers: [Answer])
For further example, it is now very simple to make a Question
in swift using the above typealias
's:
Question(Question: "What is this question?", Answers: [("A typedef", false), ("FooBar", false), ("I don't know", false), ("A Question!", true)])
I've tried using using
statements and creating abstract classes to no avail so far.
Thanks in advance.
This thread is old but I would to add a straightforward answer as the none of the existing ones are.
C# does not have an exact equivalent to typealias
:
The typealias
definition in Swift is persistent and can be reused elsewhere.
The using
definition only persists in the file where it is defined. It cannot be used in other parts of the project.
I would a class for reference types.
public class Answer
{
public string Title {get;set;}
public bool IsCorrect {get;set;}
}
or a struct for a value type
public struct Question
{
public string Question;
public Answer[] Answers;
}
Also, as an FYI the using statement is used with disposable objects to make sure the resources get released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With