Memo when learning C # (corrected at any time)

3 minute read

Standard input

//Here, the input value is assigned to the variable as it is.=Only on the right side of
//String
var date = Console.ReadLine();
//int type
var num = int.Parse(Console.ReadLine()); //int.Converting to int type with Parse

output

//No line breaks
Console.Write(/*Output value*/);
//With line breaks
Console.WriteLine(/*Output value*/);

Console.ReadKey
You can put the program closed automatically at any place, waiting for input.

Console.WriteLine("HELLO");
//Normally, HELLO is output here and the program closes, but if there is the following description, it will be in a state of waiting for input.
Console.ReadKey();

if statement

if (/*Conditional expression*/) {
  /*processing*/
} else if (/*Conditional expression*/) {
  /*processing*/
} else {
  /*processing*/
}

while statement


//First, declare a counter variable so that it does not become an infinite loop.
var i = 0;
while (/*Conditional expression of counter variable*/) {
  /*Loop processing*/
  //Update counter variables below
  i+=1
}

for statement

//After for(Declaration of counter variables;Conditional expression;Update counter variable)
for (var i = 0; i<10; i+=1) {
  /*Iterative processing*/
}

Array

//Array declaration
int ary[5]; //Element type array name[Number of elements]
//Create an array by specifying elements
string[] words = {"C#","Go","Ruby"};
int[] nums = {2,4,6,8};

//First, use the List class when you don't know how many elements to put in the array.
using System.Collections.Generic;
var ary = new List<string>();
ary.Add("C#"); //Add elements to the array created by List
ary.Insert(0,"Go"); //Insert into the array created by List Specify index as the first argument

//Handling of multidimensional arrays
string[] ary1 = {"Yamada", "Sato", "Kobayashi"};
string[] ary2 = {"Endo", "Matsuda", "Wakabayashi"};
string[][] names = {ary1,ary2}; //At the time of declaration[]Add one to put the array in the array
names[1][1] //Matsuda

Split method

Splitting strings

var name = "Yamada Taro";
string[] names = name.Split(' '); //In this case, it is divided by spaces and put in the names array.
//Arguments when splitting are deleted

string.Join method

Concatenate the elements of an array

//You can output the contents of the array at once
string[] names = {"Yamada","Yamamoto"};
Console.WriteLine(string.Join(",",names)); //Specify the delimiter between the elements in the first argument
//Yamada,Yamamoto and output

foreach
Process the contents of the array and character strings one by one

int[] nums = {1,2,3,4,5};
//Output by doubling each value in the array
foreach (int num in nums) { //foreach(Type the variable used in the foreach method Its name in The array or string to process)
  Console.Write(num*2);
}
//246810
var word = "AIUEO";
foreach (char s in word) {
  Console.WriteLine(s);
}
//One character is char type instead of string type.
//Aiueo outputs with line breaks

Length method

Get the length of an array or string

var word = "hello"
var snum = "1234567"
var num = 1234567
string[] ary = {"A","B","C"}
word.Length // 5
snum.Length // 7
num.Length //Error because the value of the variable num is not a string
ary.Length // 3

Substring method

Get the specified string from the string

var word = "ABCDEFGH";
Console.WriteLine(word.Substring(3,2)); //Index of acquisition start position in the first argument Number of characters to be acquired in the second
//DE and output

IndexOf method

Finds a character in a string or array and returns the position or index

var word = "a-I-U-E-O";
Console.WriteLine(word.IndexOf("e")); //Enter the character you want to find in the argument
//3 which is the index of the first "e" is output
Console.WriteLine(word.IndexOf("Or"));
//Output 0 if not included in the string

string[] names = {"Uchiyama","Kamiya","Ozaki"};
Console.WriteLine(Array.IndexOf(names,"Kamiya")); //Array is Array.IndexOf(Array name,The element you want to find)Describe as
//1 is output

Select method

It is close to a multilingual map that processes each element like foreach using Linq.

using System.Linq; //Now you can use Linq
int[] nums = {1,2,3,4,5};
var ary = nums.Select(x => x*2); //Assign the element of nums to x on the left side, process it on the right side, and store it in ary.
Console.WriteLine(string.Join(",",ary)); //Concatenate ary and output
//Doubled each element of nums 2,4,6,8,10 is output

Where method

Use Linq to process each element like foreach, return the one that meets the conditions

using System.Linq; //Now you can use Linq
int[] nums = {1,2,3,4,5};
var ary = nums.Where(x => x>2); //Substitute the elements of nums into x on the left side and store only those that meet or collate on the right side in ary.
Console.WriteLine(string.Join(",",ary)); //Concatenate ary and output
//3 with a value greater than 2 among the elements of nums,4,5 is output

Tags:

Updated: