Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Equivalent of Swift typealias

Tags:

c#

types

swift

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.

like image 701
HudsonGraeme Avatar asked Jan 02 '23 17:01

HudsonGraeme


2 Answers

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.

like image 87
heshuimu Avatar answered Jan 12 '23 06:01

heshuimu


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.

like image 24
Sean Avatar answered Jan 12 '23 06:01

Sean