Tutorial by Examples

is

Checks if an object is compatible with a given type, i.e. if an object is an instance of the BaseInterface type, or a type that derives from BaseInterface: interface BaseInterface {} class BaseClass : BaseInterface {} class DerivedClass : BaseClass {} var d = new DerivedClass(); Console.Write...
Returns the Type of an object, without the need to instantiate it. Type type = typeof(string); Console.WriteLine(type.FullName); //System.String Console.WriteLine("Hello".GetType() == type); //True Console.WriteLine("Hello".GetType() == typeof(string)); //True
const is used to represent values that will never change throughout the lifetime of the program. Its value is constant from compile-time, as opposed to the readonly keyword, whose value is constant from run-time. For example, since the speed of light will never change, we can store it in a constan...
The namespace keyword is an organization construct that helps us understand how a codebase is arranged. Namespaces in C# are virtual spaces rather than being in a physical folder. namespace StackOverflow { namespace Documentation { namespace CSharp.Keywords { ...
Cast is different from the other methods of Enumerable in that it is an extension method for IEnumerable, not for IEnumerable<T>. Thus it can be used to convert instances of the former into instances of the later. This does not compile since ArrayList does not implement IEnumerable<T>: ...
public interface IAnimal { string Name { get; set; } } public interface INoiseMaker { string MakeNoise(); } public class Cat : IAnimal, INoiseMaker { public Cat() { Name = "Cat"; } public string Name { get; set; } public string M...
public class LivingBeing { string Name { get; set; } } public interface IAnimal { bool HasHair { get; set; } } public interface INoiseMaker { string MakeNoise(); } //Note that in C#, the base class name must come before the interface names public class Cat : LivingBei...
interface BaseInterface {} class BaseClass : BaseInterface {} interface DerivedInterface {} class DerivedClass : BaseClass, DerivedInterface {} var baseInterfaceType = typeof(BaseInterface); var derivedInterfaceType = typeof(DerivedInterface); var baseType = typeof(BaseClass); var derived...
try, catch, finally, and throw allow you to handle exceptions in your code. var processor = new InputProcessor(); // The code within the try block will be executed. If an exception occurs during execution of // this code, execution will pass to the catch block corresponding to the exception typ...
using System; using BasicStuff = System; using Sayer = System.Console; using static System.Console; //From C# 6 class Program { public static void Main() { System.Console.WriteLine("Ignoring usings and specifying full type name"); Console.WriteLine(&quot...
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; class TcpChat { static void Main(string[] args) { if(args.Length == 0) { Console.WriteLine("Basic TCP chat"); Console.WriteLine(); ...
Immediately pass control to the next iteration of the enclosing loop construct (for, foreach, do, while): for (var i = 0; i < 10; i++) { if (i < 5) { continue; } Console.WriteLine(i); } Output: 5 6 7 8 9 Live Demo on .NET Fiddle var stuff = new [] ...
Using the null-coalescing operator (??) allows you to specify a default value for a nullable type if the left-hand operand is null. string testString = null; Console.WriteLine("The specified string is - " + (testString ?? "not provided")); Live Demo on .NET Fiddle This is l...
using is syntactic sugar that allows you to guarantee that a resource is cleaned up without needing an explicit try-finally block. This means your code will be much cleaner, and you won't leak non-managed resources. Standard Dispose cleanup pattern, for objects that implement the IDisposable interf...
The using static [Namespace.Type] directive allows the importing of static members of types and enumeration values. Extension methods are imported as extension methods (from just one type), not into top-level scope. 6.0 using static System.Console; using static System.ConsoleColor; using static ...
Overloading just equality operators is not enough. Under different circumstances, all of the following can be called: object.Equals and object.GetHashCode IEquatable<T>.Equals (optional, allows avoiding boxing) operator == and operator != (optional, allows using operators) When overrid...
To make a class support collection initializers, it must implement IEnumerable interface and have at least one Add method. Since C# 6, any collection implementing IEnumerable can be extended with custom Add methods using extension methods. class Program { static void Main() { va...
Extension methods are static methods which behave like instance methods. However, unlike what happens when calling an instance method on a null reference, when an extension method is called with a null reference, it does not throw a NullReferenceException. This can be quite useful in some scenarios....
string sqrt = "\u221A"; // √ string emoji = "\U0001F601"; // 😁 string text = "\u0022Hello World\u0022"; // "Hello World" string variableWidth = "\x22Hello World\x22"; // "Hello World"
Apostrophes char apostrophe = '\''; Backslash char oneBackslash = '\\';

Page 7 of 1336