Is there a C# counterpart to TypeScript's Template Literal Types?

Recently, I've encountered a scenario in my C# project where I need to handle a SQL sort order parameter within a function:

public static string BuildSortClause(string colId, string sortOrder) {
   ...
}

Specifically, I want to ensure that only 'ASC' or 'DESC' are accepted as valid sortOrder strings during compilation. Any other input should result in a failure. If this were Typescript, I could easily achieve this by using a Template Literal Type for the parameter like so:

type SortOrder = 'ASC' | 'DESC';

My question is: What is the most similar solution in C# to restrict the type of strings allowed for this parameter?

Answer №1

To achieve this functionality, utilize an enum along with the default ToString method.

public enum SortingType { Ascending, Descending }

public static string CreateSortingClause(string columnId, SortingType sortType) {
   // string sorting = sortType.ToString(); //either "Ascending" or "Descending"
   ...
}

public static void Execute() {
   CreateSortingClause("someColumnId", SortingType.Ascending);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The PrimeNG Divider Component's content background color

Having trouble changing the background color of text inside a Divider provided by PrimeNG. The entire section has a background color applied through a class added to a div container: div However, this background color does not extend to the Text within th ...

Troubleshooting Date Errors in Typescript with VueJS

Encountering a peculiar issue with Typescript while attempting to instantiate a new Date object. <template> <div> Testing Date</div> </template> <script lang="ts"> import Vue from "vue"; export default Vue.extend({ name: ...

In Angular, encountering difficulty accessing object members within an array when using custom pipes

Here is a custom pipe that I have created, but I am facing an issue accessing the members of the customfilter array, which is of type Item. import { Pipe, PipeTransform } from '@angular/core'; import {Bus} from '/home/pavan/Desktop/Pavan ...

A step-by-step guide on integrating Detox with jest (ts-jest) and Typescript in a react-native app

I've been experimenting with incorporating Typescript into my detox tests. The most relevant information I could find was in this gist. However, when trying to implement it, I encountered an error stating that jasmine is not defined. After researching ...

Unable to simultaneously execute TypeScript and nodemon

Currently, I am in the process of developing a RESTful API using Node.js, Express, and TypeScript. To facilitate this, I have already installed all the necessary dependencies, including nodemon. In my TypeScript configuration file, I made a modification to ...

Explore the titles provided by Wikipedia

Hi there, I'm fairly new to Angular and trying to work with the Wikipedia API. However, I seem to be having trouble getting 4 titles from the API. Here's an example URL for one of the titles: https://en.wikipedia.org/w/api.php?action=query&pr ...

Watching - transforming / combining

I am a beginner when it comes to working with Observables. Here's the Effect that I am using: My goal is to dispatch the PositionUpdateAction or PositionFailedAction before the SunriseSunsetAction is dispatched. Currently, what happens is that the r ...

Making sure that text is displayed properly within a dataGridView column by enabling text wrapping

My dataGridView contains a specific column that displays long text. However, this text is shown in a shortened version with ellipses due to the column's width limitations. | textdsadasda... | How can I make my dataGridView display this text on the ...

Guidance on JSON data structuring

I am currently working on implementing pagination for a WebApi that I am developing. I need to include the total number of records and the requested display amount in the JSON response. UPDATE After considering your suggestions, I tried serializing my ob ...

The operator is being invoked multiple times beyond originally anticipated

I am currently working on developing code that paginates a result set using the expand operator until a specific number of resources have been fetched. Below is the code snippet I have written so far (excluding the actual async call logic): import { Obser ...

Is there a way for a variable's value to mirror the value of another variable?

Here's a simplified example of my current code: public static class Settings { public static TH th; } public partial class PhrasesFrame { private void SetC1Btn() { var a = (int)Settings.th; vm.C1BtnLabelTextColor = phrase ...

Learn the process of assigning a value to a dynamically created textbox using JavaScript in code behind

To create a textbox in the code behind, I use the following method: TextBox txt = new TextBox(); txt.ID = "txtRef" + count + dr["DataField"].ToString(); div.Controls.Add(txt); I have been attempting to set the value for this textbox within a jQuery funct ...

Styling Excel Columns in Angular

Is there a way to make the column titles bold in an Excel worksheet using the json_to_sheet function? Specifically, how can I bold the ID or format the first cell? var ws = XLSX.utils.json_to_sheet([{ID:"ID"}], {header: ["ID"], skipHeader: true}); let exce ...

Is there a way to submit multiple models concurrently in a single form?

Currently, I am developing an online lesson plans application for a special project. The lesson plan comprises the following models (which are generated using Entity Framework in a Database First approach): public partial class Subject { public int Id ...

Validating Date and Time in Angular 6 using ngIf within an HTML template

I am currently utilizing Angular6. Displayed below is the ngfor with a div. The API provides the meeting date and time along with the status, which is usually listed as upcoming. For example, if the date is 09/18/2018 and the time is 5.30 PM. <div cl ...

Guide on Sending a JSON Response from an ASP.NET MVC Controller

As a developer working in a controller, I encounter the need to transfer an entity object (Product) back to a view for JavaScript usage. The process involves passing a model object from the action method to the view. While the model object includes necess ...

Custom authorization groups in the MVC framework

I've been searching for information on implementing a custom group authorization policy in an MVC application that I'm working on, but it seems like this is not a common question or topic. In my scenario, I need to create groups dynamically where ...

Tips for sending a JSON array from a controller to JavaScript using AJAX in a view file

I've been struggling to send an array of Document objects from a method (let's say it's in a controller) to the JavaScript function where the method is being called (in a view). Oddly enough, the method refuses to pass the array - it only wo ...

Refresh GridView using a WebMethod

I'm struggling with an issue that seems a bit complex. I have a WebMethod that performs certain tasks from within a js file. One of these tasks involves calling functions from a class within the app_code directory. Within this app_code class, I retri ...

How to extract text from a <th> element with c#

Help needed in retrieving text from an HTML table We attempted to use the following method: tblGridHeader.Rows[0].InnerText.ToString() However, encountered this error message: "HTMLTableRow" does not support InnerText property. We also tried using ...