neo-project / neo-devpack-dotnet

NEO Development Pack
MIT License
78 stars 100 forks source link

C# Language Specification Support #819

Open Jim8y opened 8 months ago

Jim8y commented 8 months ago

C# Language Specification Support

Although Neo claims that smart contract development can be done using C#, the issue is that Neo does not support all C# syntax rules. This often creates confusion for C# developers, as they are not aware of which syntaxes are supported and which are not. Moreover, since C# itself is not designed for smart contract development, some syntaxes may behave inconsistently when developing contracts. To address this issue, I suggest that we initially use the standard of the C# language as our benchmark for contract development. Based on the C# standard, we should try to support a more complete C# syntax for contract development as much as possible. For those C# syntax features that are inconsistent or unsupported, we need to have clear documentation to introduce them.

C# Language Specification : https://ecma-international.org/publications-and-standards/standards/ecma-334/

C# Syntax Reference

1. Keywords

Basic Data Types (https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.CSharp/Helper.cs)

C# to Neo smart contract type mapping table:

C# Type Neo Type Description
bool Boolean Boolean type
byte Integer 8-bit unsigned integer
sbyte Integer 8-bit signed integer
short Integer 256-bit signed integer-le
ushort Integer 256-bit signed integer-le
int Integer 256-bit signed integer-le
uint Integer 256-bit signed integer-le
long Integer 256-bit signed integer-le
ulong Integer 256-bit signed integer-le
char Integer Unicode character
string ByteString String
byte[] ByteArray Byte array
BigInteger Integer 256-bit signed integer
Enum types Integer Enum underlying integer mapping
Array types Array Array
object Any Any type
void Void No return value
Neo.Cryptography.ECC.ECPoint PublicKey Represents public key
Neo.SmartContract.Framework.ByteString ByteString Byte string
Neo.UInt160 Hash160 20-byte hash value
Neo.UInt256 Hash256 32-byte hash value
Other classes/interfaces ByteArray Stored as byte arrays

Classes and Structures

Control Flow Keywords (https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.CSharp/MethodConvert.cs)

Access Modifiers and Member Control

Type Conversion and Checking

Parameters and Methods

Special Keywords

Contextual Keywords

2. Operators (https://github.com/neo-project/neo-devpack-dotnet/blob/master/src/Neo.Compiler.CSharp/MethodConvert.cs)

Arithmetic Operators

Comparison Operators

Logical Operators

Bitwise Operators

Assignment Operators

Other Operators

3. Data Types

Value Types

Reference Types

4. Syntactic Sugar and Advanced Features

5. Other Features

6. Compilation Directives and Special Symbols

Compilation Directives

Special Symbols

Numeric Type Conversions:

Common Mathematical Methods and Functions in C

Basic Arithmetic Operations:

Exponentiation and Logarithms:

Trigonometric Functions:

Rounding and Precision:

Absolute and Sign:

Random Number Generation:

Constants:

Additional Functions:

Common String Methods in C

  1. String Length:
    • [x] string.Length: Returns the length (number of characters) of a string.
  2. Substring Extraction:
    • [x] string.Substring(startIndex): Returns a substring starting from the specified startIndex to the end of the string.
    • [x] string.Substring(startIndex, length): Returns a substring starting from the specified startIndex with the specified length.
  3. Concatenation:
    • [ ] string.Concat(str1, str2, ...): Concatenates multiple strings together.
  4. String Interpolation:
    • [ ] $"...": Allows you to embed expressions and variables directly within a string.
  5. String Formatting:
    • [ ] string.Format(format, arg0, arg1, ...): Formats a string with placeholders and values.
  6. String Comparison:
    • [ ] string.Equals(str1, str2): Compares two strings for equality.
    • [ ] string.Compare(str1, str2): Compares two strings and returns a value indicating their relative order.
  7. String Searching and Checking:
    • [ ] string.Contains(substring): Checks if a string contains a specified substring.
    • [ ] string.StartsWith(prefix): Checks if a string starts with a specified prefix.
    • [ ] string.EndsWith(suffix): Checks if a string ends with a specified suffix.
  8. String Manipulation:
    • [ ] string.ToUpper(): Converts the string to uppercase.
    • [ ] string.ToLower(): Converts the string to lowercase.
    • [ ] string.Trim(): Removes leading and trailing whitespace.
  9. String Splitting:
    • [ ] string.Split(delimiters): Splits a string into an array of substrings based on specified delimiters.
  10. String Replacement:
    • [ ] string.Replace(oldValue, newValue): Replaces all occurrences of oldValue with newValue.
  11. String Empty or Null Check:
    • [ ] string.IsNullOrEmpty(str): Checks if a string is either null or empty.
    • [ ] string.IsNullOrWhiteSpace(str): Checks if a string is null, empty, or contains only whitespace.

Common Char Methods and Properties in C

Character Representation

Char Comparison

Char Conversions

Char Testing

Char Case Conversions

Common LINQ Query Methods and Operations in C

LINQ is a powerful language feature in C# that allows you to query and manipulate collections of data. Here are some common LINQ methods and operations:

Certainly! Here's the provided content with empty checkboxes:

Query Methods

  1. [ ] Where: Filters a sequence of elements based on a given condition.

    • Example: var result = numbers.Where(x => x > 5);
  2. [ ] Select: Transforms each element of a sequence into a new form.

    • Example: var result = names.Select(name => name.ToUpper());
  3. [ ] OrderBy and OrderByDescending: Sorts elements in ascending or descending order based on a specified key.

    • Example: var result = numbers.OrderBy(x => x);
  4. [ ] GroupBy: Groups elements based on a key and returns groups of elements.

    • Example: var result = products.GroupBy(product => product.Category);
  5. [ ] Join: Combines two collections based on a common key.

    • Example: var result = customers.Join(orders, customer => customer.Id, order => order.CustomerId, (customer, order) => new { customer.Name, order.OrderDate });
  6. [ ] Any and All: Checks if any or all elements in a sequence satisfy a condition.

    • Example: bool anyPositive = numbers.Any(x => x > 0);
  7. [ ] First, FirstOrDefault, Last, and LastOrDefault: Retrieves the first or last element in a sequence, optionally with a condition.

    • Example: var firstPositive = numbers.First(x => x > 0);
  8. [ ] Single, SingleOrDefault: Retrieves the single element in a sequence that satisfies a condition.

    • Example: var singlePositive = numbers.Single(x => x > 0);

Set Operations

  1. [ ] Union: Combines two sequences, removing duplicates.

    • Example: var result = sequence1.Union(sequence2);
  2. [ ] Intersect: Returns the common elements between two sequences.

    • Example: var result = sequence1.Intersect(sequence2);
  3. [ ] Except: Returns elements that are present in one sequence but not in another.

    • Example: var result = sequence1.Except(sequence2);

Aggregation

  1. [ ] Count: Returns the number of elements in a sequence.

    • Example: int count = numbers.Count();
  2. [ ] Sum, Min, and Max: Calculates the sum, minimum, or maximum value in a sequence.

    • Example: int sum = numbers.Sum();
  3. [ ] Average: Computes the average value of elements in a sequence.

    • Example: double average = numbers.Average();
  4. [ ] Aggregate: Performs a custom aggregation operation on elements in a sequence.

    • Example: var result = numbers.Aggregate((x, y) => x * y);

Conversion

  1. [ ] ToList and ToArray: Converts a sequence to a list or an array.

    • Example: List<int> list = numbers.ToList();
  2. [ ] ToDictionary: Converts a sequence into a dictionary based on key and value selectors.

    • Example: Dictionary<int, string> dict = items.ToDictionary(item => item.Id, item => item.Name);
  3. [ ] OfType: Filters elements of a sequence to include only those of a specific type.

    • Example: var result = mixedObjects.OfType<string>();
  4. [ ] Cast: Casts elements of a sequence to a specified type.

    • Example: var result = mixedObjects.Cast<int>();

Valid C# Statements

  1. [x] Variable Declaration and Assignment:

    • int x = 10;
    • string name = "John";
  2. [x] Expression Statements:

    • x = x + 5;
    • Console.WriteLine("Hello, World!");
  3. [x] Conditional Statements:

    • if (condition)
    • if (condition) { ... }
    • else
    • else if (condition) { ... }
    • switch (variable) { ... }
  4. [x] Loop Statements:

    • for (int i = 0; i < 10; i++) { ... }
    • while (condition) { ... }
    • do { ... } while (condition);
    • foreach (var item in collection) { ... }
  5. [x] Jump Statements:

    • break;
    • continue;
    • return value;
    • goto label;
  6. [x] Exception Handling:

    • try { ... }
    • catch (ExceptionType ex) { ... }
    • finally { ... }
    • throw new Exception("Error message");
  7. [x] Method Calls:

    • Method();
    • int result = Add(5, 3);
  8. [x] Object Creation and Initialization:

    • MyClass obj = new MyClass();
    • MyClass obj = new MyClass { Property1 = "Value1", Property2 = "Value2" };
  9. [ ] Lock Statement:

    • lock (lockObject) { ... }
  10. [x] Checked and Unchecked Statements:

    • checked { ... }
    • unchecked { ... }
  11. [ ] Using Statement (for Resource Management):

    • using (var resource = new Resource()) { ... }
  12. [x] Delegates and Events:

    • delegate void MyDelegate(int x);
    • event EventHandler MyEvent;
  13. [ ] Lambda Expressions:

    • (x, y) => x + y
    • (x) => { Console.WriteLine(x); }
  14. [ ] Attribute Usage:

    • [Attribute]
    • [Obsolete("This method is deprecated")]
  15. [ ] Unsafe Code (for Pointer Operations):

    • unsafe { ... }
  16. [ ] Asynchronous Programming (async/await):

    • async Task MyMethod() { ... }
    • await SomeAsyncOperation();
  17. [ ] Yield Statement (for Iterator Methods):

    • yield return item;
  18. [ ] Pattern Matching (C# 7.0+):

    • if (obj is int number) { ... }
  19. [ ] Local Functions (C# 7.0+):

    • int Add(int a, int b) { return a + b; }
  20. [ ] Record Types (C# 9.0+):

    • record Person(string FirstName, string LastName);
  21. [x] Nullable Types:

    • int? nullableInt = null;
  22. [x] Switch Expressions (C# 8.0+):

    • var result = variable switch { ... };
  23. [x] Interpolated Strings (C# 6.0+):

    • string message = $"Hello, {name}!";
  24. [x] Range and Index Operators (C# 8.0+):

    • var subArray = myArray[1..4];
  25. [x] Pattern-Based Switch Statements (C# 9.0+):

    • int result = variable switch { ... };
  26. [x] Discard (_) (C# 7.0+):

    • _ = SomeMethod();

Certainly! Here's the provided content with empty checkboxes:

Valid C# Patterns

Constant Patterns

Type Patterns

Var Pattern

Wildcard Pattern

Null Pattern (C# 8.0+)

Property Pattern (C# 8.0+)

Tuple Pattern (C# 7.0+)

Positional Pattern (C# 8.0+)

Recursive Pattern (C# 8.0+)

Logical Patterns (C# 9.0+)

Type Patterns with When Clause (C# 7.0+)

Var Pattern with When Clause (C# 7.0+)

Binary Patterns (C# 9.0+)

Parenthesized Patterns (C# 8.0+)

Relational Patterns (C# 9.0+)

Valid C# Expressions

Primary Expressions

Member Access Expressions

Invocation Expressions

Element Access Expressions

Object Creation Expressions

Lambda Expressions

Anonymous Types (C# 3.0+)

Object Initialization Expressions

Collection Initialization Expressions (C# 3.0+)

Array Initialization Expressions

Nullable Value Types

Type Conversion Expressions

Arithmetic Expressions

Relational Expressions

Logical Expressions

Conditional Expressions

Assignment Expressions

Increment and Decrement Expressions

Common BigInteger Methods and Properties

Constructors

Static Methods

Properties

Conversion Methods

Jim8y commented 1 month ago

Need an update to C# 12 features.