C # grammar memo

4 minute read

Array

Array declaration
Type name [] = variable name;

Specifying the number of elements
Variable name = new type name [number of elements];

Declaration of array and specification of number of elements
Type name [] Variable name = new Type name [number of elements];

Array initialization (array declaration and value assignment)
Type name [] Variable name = {value, value, value};

foreach statement

foreach (Type name Variable name in Array variable name) 
{
}
foreach (KeyValuePair<Model name, Model name>Variable name in Dictionary type variable name)
{
//Variable name.Key、Variable name.You can get Key and Value with Value
}

List

Create a List type variable
List <type name> Variable name = new List <type name> ();

Add element to List
Variable name.Add (value to add);

Get the number of elements in List
Variable name.Count;

Remove element from List
Variable name.RemoveAt (index)
Variable name.RemoveAt (value you want to remove)

  • If there are multiple elements with the same value, the element with the smallest index is deleted.

Sort the List in ascending order
Variable name.sort ();

Dictionary

Create a dictionary type variable
Dictionary <key type, value type> Variable name = new Dictionary <key type, value type> ();

Add element to Dictionary
Variable name.Add (key, value);

Get the value corresponding to the key from Dictionary
Variable name [key]

int type

TryParse (string type value, variable to which the converted value is assigned) method

Converts string type to int type and returns true if it can be converted correctly.

Parse (string value) method

Convert from string type to int type.

string type

Split (‘character’) method

Separate with the character given to the argument to make a string type array.

Contains (‘character’) method

Judge whether the character specified by the argument is included in the judgment character string

StartsWith (‘character’) method

Judgment whether the character specified by the argument is included at the beginning of the judgment character string

EndWith (‘character’) method

Judgment whether the character specified by the argument is included at the end of the judgment string

StreamReader type method

ReadLine method

Returns as a one-line read string from the file.

DataSet type

Data set name (* starting with lowercase letters). Data table name. Add data table name Row (data for one row to be registered) method

Add value to DataTable

Pass value type arguments as reference type

ref modifier … You should assign a value to the variable passed to the argument in advance.
out modifier … You don’t have to assign a value to the variable passed to the argument in advance.

DataGridView type

currentRow.Index property

Get index of selected row

Properties

Mechanism for creating accessors (getters, setters)
Write code more concisely than using accessors directly

Public property type name Property name
{
  set
  {
Variable name= value;
  }
  get
  {
return variable name;
  }
}

LINQ

You can extract only the values that satisfy the conditions from the array or collection, and perform specific processing on each data.

Extract only the data that meets the lambda expression conditions from the array or collection
var variable name = array or collection variable name.Where (lambda expression)

  • Var … The compiler automatically determines the type based on the assigned value.

Create new data by adding lambda expression processing to array and collection data
var variable name = array or collection variable name.Select (lambda expression)

Get the number of elements
var variable name = array or collection variable name.Count ()

Overload

Multiple methods with the same name can be defined when the argument type and number are different

Override

How to write methods for overridden base classes

virtual return type method name()
{
Method processing content
}

How to write a method for a derived class to override

override Return type Method name()
{
Method processing content
}

Abstract classes and methods

Abstract method-Method processing is an empty method that is supposed to be implemented in a derived class.
Abstract class … A class with one or more abstract methods

Base class

abstract class class name//Cannot instantiate this class with abstarct
{
public abstract Return type Method name(); 
}

Derived class

class class name:Base class name
{
//Error if you do not write the contents of the abstract method
public override Return type Method name()
  {
Processing content
  }
}

static

static … If you attach it to a member variable or member method of a class, you can call them without creating an instance.

class Employee
{
public static string companyName;
}
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Employee.companyName);
        }
    }

partial

When writing a class in two files, put it before the class.

public partial class Form1 : Form
{
abridgement
}

Structure

Structure: Value type ↔︎ Class: Reference type
ex) Point type that represents 2D coordinates, Vector type that represents a vector, Color type that represents a color

struct structure name
{
Member variables
Member method
}

using statement

Call the Dispose method of the class when exiting the using block.
In the following cases, the file is closed by the Dispose method.

            using (System.IO.StreamReader file =
                new System.IO.StreamReader(@"..\..\data.txt"))
            {
                while (!file.EndOfStream)
                {
                    string line = file.ReadLine();
                    string[] data = line.Split(',');
                    this.phonebook.Add(data[0], data[1]);
                }
            }

//Character strings with @ are special symbols (escape sequences).\It is treated as a character string that does not use n etc.)

Tags:

Updated: