Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Latest commit

 

History

History
76 lines (57 loc) · 1.59 KB

index.md

File metadata and controls

76 lines (57 loc) · 1.59 KB
extends section title description
_layouts.documentation
content
C#
C# Dev Snippets

C#

Shuffle an Array

Method to shuffle an array of strings in place.

private void Shuffle(string[] e)
{
	var rnd = new Random();
	string temp;
	for (int i = e.Length - 1; i > 0; i--)
	{
		int j = rnd.Next(0, i);
		temp = e[i];
		e[i] = e[j];
		e[j] = temp;
	}
}

C#

C# Strings

Strings are used for storing text.

string greeting = "Hello";

String Length

A string in C# is actually an object, which contain properties and methods that can perform certain operations on strings. For example, the length of a string can be found with the Length property:

string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);

Other Methods

There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of the string converted to uppercase or lowercase:

string txt = "Hello World";
Console.WriteLine(txt.ToUpper());   // Outputs "HELLO WORLD"
Console.WriteLine(txt.ToLower());   // Outputs "hello world"

String Concatenation

The + operator can be used between strings to combine them. This is called concatenation:

string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);

You can also use the string.Concat() method to concatenate two strings:

string firstName = "John ";
string lastName = "Doe";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);