Skip to main content
Skip table of contents

Writing C# Rules

C# is a programming language that creates robust and secure applications for Windows, web, mobile, and other platforms. C# applications are typically developed using Microsoft's .NET framework, which provides a large library of pre-built functionality and components and a development environment for creating custom applications.

C# applications can be developed in various ways, from a basic console application to fully featured enterprise applications. They can be used for various purposes, from simple web games to complex software solutions.


Restrictions

  1. All code must be contained within a class.

  2. All code must be contained within a namespace.

  3. All identifiers (variables, types, methods, classes, etc.) must be named using Pascal casing.

  4. All code must be surrounded by a pair of curly braces.

  5. All code must be encapsulated within one or more classes.

  6. All code must be written using proper indentation and whitespace.

  7. All variables must be declared before they are used.

  8. All variables must be declared with an appropriate data type.

  9. All classes must be declared using the sealed keyword unless they are meant to be extended.

  10. All classes must be declared with the public access modifier unless otherwise specified.

  11. All methods must be declared using the correct access modifier.

  12. All methods must have a return type, even if it is void.

  13. All method parameters must be declared with the correct data type.

  14. All variables must be assigned a value.

  15. All string literals must be enclosed in double-quotes.

  16. All boolean literals must be written using the keywords true and false.

Best Practices

  • Use meaningful names: When naming variables, methods, parameters, and classes in your code, ensure they reflect what they are used for. This will make it much easier to understand the code and help to prevent confusion and errors.

  • Use comments: Adding comments to your code can be very helpful in understanding the code and ensuring it is maintainable. Comments should be used to explain the purpose of a code section, provide information about complex sections of code, or provide a reminder of something.

  • Keep it simple: Ensure your code is as basic and straightforward as possible. It should be easy to read, understand, and maintain. Complex and redundant code can be difficult to debug and maintain, so it is important to keep things simple.

  • Don’t repeat yourself: When writing code, try to avoid repeating yourself. Don’t write the same code multiple times throughout a program. Instead, use functions and methods to reduce redundancy and keep your code clean and organized.

  • Test your code: Before you deploy your code, make sure you test it thoroughly. This will help to ensure that the code is working as intended and that all bugs are fixed before it is released.

  • Refactor when necessary: Refactoring is the process of restructuring code to make it easier to maintain. It is important to refactor code when needed to keep it organized and readable.

  • Use resources: Make sure you take advantage of the available resources. Look up documentation, tutorials, and best practices to write the best code possible.

Sample Code

Below is an example of a C# business rule that can be used to create a function:

C# Rule Example

C#
var token = request.AsString("token");
var domain = request.AsString("domain");
var cmsUrl = request.AsString("cmsUrl");;           
  
//Validate required parameters here
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(token, "token");
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(domain, "domain");
ASF.Framework.Util.ValidationHelper.ValidateNotNullString(cmsUrl, "cmsUrl");
  
var resource = new ASF.CoreLib.Messages.GetCmsResourceResponse();
  
try
{
    //get resource
    resource = ASF.CoreLib.APIHelper.GetCmsResource(new ASF.CoreLib.Messages.GetCmsResourceRequest()
    {                 
        Domain = domain,                 
        Token = token,
        Url = cmsUrl
    });
      
    if(!string.IsNullOrEmpty(resource.ErrorMessage))
    {
        ASF.Framework.Util.LogHelper.Error(ASF.CoreLib.APIHelper.Logger, resource.ErrorMessage);
        throw new InvalidOperationException("Unhandled Error");
    }
    else
    {                     
        resource.Resource.Stream.Seek(0, System.IO.SeekOrigin.Begin);
        using (System.IO.StreamReader sr = new System.IO.StreamReader(resource.Resource.Stream))
        {
            response["outputText"] = sr.ReadToEnd();
        }
    }
}
catch (Exception e)
{
    ASF.Framework.Util.LogHelper.Error(ASF.CoreLib.APIHelper.Logger, "Unhandled Error" + e);
    response.Result.AddParameter(new ASF.Framework.Service.Parameters.Parameter { Name = "Error", Value = e.ToString() });
}
finally
{
    resource.Resource.Stream.Close();
}


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.