Some pitfalls encountered when using C# for Grasshopper (long-term update)

Foreword:

This article is a note-taking article that will be updated for a long time, mainly used to record some pitfalls encountered in C#4GH

This article inherits from the following parent class

Some pitfalls encountered in C# programming (long-term update) - Yanwu KAHB's Blog - CSDN Blog

Table of contents

Foreword:

1. About for loop nesting

2. How to output content at the out end of the C# battery block in Grasshopper


1. About for loop nesting

1.1 Non-repeated pairwise correspondence

List<Line> neo = new List<Line>();
    for(int i = 0;i < x.Count - 1;i++){
      for(int j = i + 1;j < x.Count;j++){
        neo.Add(new Line(x[i], x[j]));
      }
    }
A = neo;

1.2 Repeated pairwise correspondence

List<Line> neo = new List<Line>();
    for(int i = 0;i < x.Count;i++){
      for(int j = 0;j < x.Count;j++){
        neo.Add(new Line(x[i], x[j]));
      }
    }
A = neo;

1.3 Remove the correspondence between yourself and yourself in the repeated pairwise correspondence (add a judgment)

List<Line> neo = new List<Line>();
    for(int i = 0;i < x.Count;i++){
      for(int j = 0;j < x.Count;j++){
        if(j != i){
          neo.Add(new Line(x[i], x[j]));
        }        
      }
    }
A = neo;

Note: This statement cannot be written in C#

The reasons are as follows:  (Image source: Liu Tiemeng C# tutorial)

 ! = Participate in the calculation first, when j != i is false, the statement stops and does not continue to increase

2. How to output content at the out end of the C# battery block in Grasshopper

Use the Print() method

Guess you like

Origin blog.csdn.net/qq_41904236/article/details/124914058