i am has test C#10 in visual studio 2022 in autocad 2019 64 bit but not all code work as well example indexing of array in last of my code
see snapshot
how to make all code work fine in C#10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices.Filters;
using System.Collections;
using static cadtest22_1.PersonClass;
namespace cadtest22_1
{
//1.test create struct Tipe T
public struct Foo<T>
{
public T Var1;
public T Var2;
}
public struct Point
{
public double X { get; set; }
public double Y { get; set; }
public double Distance => Math.Sqrt(X * X + Y * Y);
public override string ToString() =>
$"({X}, {Y}) is {Distance} from the origin";
}
//2. test record
public class PersonClass
{
public string Name { get; }
public string Surname { get; set; }
public record PersonRecord
{
public string Name { get; set; }
public string Surname { get; set; }
public PersonRecord(string name, string surname)
{
Name = name;
Surname = surname;
}
public void Deconstruct(out string name, out string surname)
{
name = Name;
surname = Surname;
}
}
}
public class Class1
{
//3.test call
int M()
{
int y = 5;
int x = 7;
return Add(x, y);
static int Add(int left, int right) => left + right;
}
[CommandMethod("test")]
public void test1()
{
var doc = CadApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
//call struct foo
var d = new Foo<int> { Var1 = 1, Var2 = 2 }; //ok
var dd = new Foo<double> { Var1 = 1.1, Var2 = 20.1 }; //ok
CadApp.ShowAlertDialog($"struct foo T {d.Var1} - {d.Var2}"); //ok
// test switch multiple variable
string RockPaperScissors(string first, string second)
=> (first, second) switch
{
("rock", "paper") => "rock is covered by paper. Paper wins.",
("rock", "scissors") => "rock breaks scissors. Rock wins.",
("paper", "rock") => "paper covers rock. Paper wins.",
("paper", "scissors") => "paper is cut by scissors. Scissors wins.",
("scissors", "rock") => "scissors is broken by rock. Rock wins.",
("scissors", "paper") => "scissors cuts paper. Scissors wins.",
(_, _) => "tie"
};
CadApp.ShowAlertDialog($"{RockPaperScissors("rock","paper")}");
//add function without aggregate,action
int M()
{
int y = 5;
int x = 7;
return Add(x, y);
static int Add(int left, int right) => left + right;
} //ok
//test multiple switch
static int GetTax4(int muncipalityId) => muncipalityId switch
{
0 or 1 => 20,
> 1 and < 5 => 21,
> 5 and not 7 => 22,
7 => 23,
_ => 20
};
CadApp.ShowAlertDialog("value switch " + GetTax4(6).ToString());//ok
// test switch to autocad
static string ent(Entity myent) => myent.GetType().Name.ToString() switch
{
"Line" or "Polyline" or "Circle" => "curve",
_ => "Not curve"
};
var ge = ed.GetEntity("\nclick entiti");
if (ge.Status != PromptStatus.OK) return;
using(var tr = db.TransactionManager.StartTransaction())
{
var ent1 = tr.GetObject(ge.ObjectId, OpenMode.ForRead) as Entity;
string result = ent(ent1);
CadApp.ShowAlertDialog($"value switch {result}"); //ok
// test nullable
List<int> numbers = null;
int? i = null;
numbers ??= new List<int>();
numbers.Add(i ??= 17);
numbers.Add(i ??= 20);
// test nullable in autocad
var cur = ent1 as Curve;
Point3d? pt = cur?.StartPoint ?? (cur as Circle)?.Center;
CadApp.ShowAlertDialog($"value {cur.GetType().Name} {pt}");
}
// tes bool function is and or
bool IsLetter(char c) => c is >= 'a' and <= 'z' or >= 'A' and <= 'Z';
CadApp.ShowAlertDialog("is true :" + IsLetter('a').ToString());//ok
var person = new PersonRecord("Silver", "Chariot");
var (name, surname) = person;
CadApp.ShowAlertDialog($"name {name} and surname {surname}");
//testing lambda discard parameter
Application.BeginDoubleClick += (_,_)=> CadApp.ShowAlertDialog("testing dobule click"); //ok
[b] var value = new[] { 10, 11, 12, 13 };
//this not working system index not import what must to do
int a = value[^1]; // 13
int b = value[^2]; // 12
int c = value[2..3];[/b]
}
}
}