0% found this document useful (0 votes)
74 views

C Sharp Quick Ref

This document provides a summary of C# programming concepts including: 1. An overview of the history and features of different C# versions such as generics in C# 2.0 and asynchronous methods in C# 5.0. 2. Descriptions of common C# control structures like if/else statements, for loops, and try/catch blocks. 3. Brief definitions of C# data types including value types stored on the stack and collections like arrays.

Uploaded by

matias peronetto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

C Sharp Quick Ref

This document provides a summary of C# programming concepts including: 1. An overview of the history and features of different C# versions such as generics in C# 2.0 and asynchronous methods in C# 5.0. 2. Descriptions of common C# control structures like if/else statements, for loops, and try/catch blocks. 3. Brief definitions of C# data types including value types stored on the stack and collections like arrays.

Uploaded by

matias peronetto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

C# Quick Ref 2. Control Structures


if (condition)
try
{
// Do something
Review notes for the most often forgotten things {
}
Compiled by Bruce Hantover -- August 2016 (ver 823) // Do something catch(Exception e)
} {
Public Domain notice: I took this info from various sources on the Console.WriteLine(e.Message);
internet, and assume that everything here belongs to the public domain.
else if }
{
// Do something else finally
{
Contents
}
// Do something
else }
{
1. History (2005 - 2016) // Do something else
} throw new Exception("blah, blah, blah ...");
2. Control Structures (if, for, while, try) ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
3. Data Structures (val, ref, structs, etc) switch (myString)
for (int i = 0; i < 100; i++)
4. Collections (arrays, lists, generics, dictionaries) { {
5. Classes (static vs. instantiated, encapsulation, // Do something case "abc":
} // Do something
inheritance, polymorphism)
break;
6. LINQ and Lambda (examples, very incomplete) case "def":
foreach (string item in itemsToWrite)
7. Misc Stuff (string functions, anonomous methods) {
// Do something
break;
// Do something
default:
1. History }

// Break exits from for, foreach, while, do .. while loops


// Do something
break;
}
C# 2.0 features (2005) for (i = 0; i < 10; i++) // see the comparison, i < 10
{ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
► Generics, Partial types, Anonymous methods, Iterators,
Nullable types, Getter/setter separate accessibility, Method group
if (i >= 3) Conditional Operator
{
conversions (delegates), Static classes, Delegate inference t ? x : y – if test t is true, then evaluate and return x;
break; // leaves the loop instantly otherwise, evaluate and return y.
C# 3.0 features (2007) }
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
► Language Integrated Queries (LINQ), Implicitly typed }
local variables, Object and collection initializers, Auto-Implemented
properties, Anonymous types, Extension methods, Query expressions, // continue skips rest of loop, goes back to condition test
Lambda expressions, Expression trees, Partial methods ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
C# 4.0 features (2010) while (condition) { Do something }
► Dynamic programming, Named and optional
parameters, Covariance and Contravariance ► do ... while does first execution before comparison
C# 5.0 features (2012)
► Asynchronous methods, Caller info attributes do { Do something }
(continued next page)
while (condition);
C# 6.0 features (July 2015, .Net 4.6) ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
► Exception filters, finally, interpolated strings, auto-property
initializers, accessors with different visibility, nameof operator
Dictionary initializer
C# 7.0 features (planned)
► Tuples, local functions (defined within a method)
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
3. Data Structures The built-in C# type aliases and their equivalent .NET Framework types follow:

Integers 4. Collections
C# Alias Type (bits) Range
Value types (stored in the stack) sbyte SByte 8 -128 to 127 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
byte Byte 8 0 to 255
The value types in the .NET framework are usually small, frequently
used types. The benefit of using them is that the type requires very short Int16 16 -32,768 to 32,767
Arrays --size can be set by variable but can't change
little resources to get up and running by the CLR. Value types do not ushort UInt16 16 0 to 65,535 int[] integers = new int[20];
require memory to be allocated on the heap and therefore will not char Char 16 unicode 0 to 65,535 double[] doubles = new double[myVariable];
cause garbage collection. However, in order to be useful, the value int Int32 32 - + 2 billion
types (or types derived from it) should remain small - ideally below uint UInt32 32 0 to 4 billion ► Multidimensional
16 bytes of data. long Int64 64 - + 9 quintillion to int[,] test = new int[2, 3];
ulong UInt64 64 0 to 18 quintillion
►Value types are always copied (intrinsically) before being passed to ► Initialized at time of creation
a method. Changes to this new object will not be reflected back in the Floating-point
original object passed into the method. C# Alias .NET Type (bits) Precision Range int[] arr = new int[] { 24, 2, 13, 47, 45 };
float Single 32 7 digits E -45 to 38
►Value types do not /need/ you to call their constructor. They are double Double 64 15-16 digits E -324, 308 ► Arrays are passed by reference, not by value
automatically initialized. decimal Decimal 128 28-29 decimal - + E28
►Value types always initialize their fields to 0 or null. ► Arrays size will be arr.Length
Other predefined types
►Value types can NEVER be assigned a value of null (unless they C# Alias .NET Type (bits) Range // example: to copy first 3 elements of array:
are declared as Nullable Yypes) bool Boolean 32 true or false var sourceArray = new int[] { 11, 12, 3, 5, 2, 9, 28, 17 };
►Value types sometimes need to be boxed (wrapped inside an object Object 32/64 a pointer to an object var destinationArray= new int[3];
string String 16*length unicode, no upper bound.
object), allowing their values to be used like objects. Array.Copy(sourceArray, 0, destinationArray, 0, 3 );
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Reference types (stored in the heap)
enum Weekday { Monday, Tuesday, ... Sunday };
Reference types are managed very differently by the CLR. All
Weekday day = Weekday.Monday;
Array List
reference types consist of two parts: A pointer to the heap (which ► ArrayList before generics used boxing / unboxing.
contains the object), and the object itself. Reference types are slightly ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ This was slow & mix-and-match was not good practice.
heavier weight because of the management behind the scenes needed
to keep track of them. Reference types are nullable. struct is a light-weight object (value type), classes are ref types.
ArrayList myAL = new ArrayList();
Structs work best if < 17 bytes of data. If in doubt, use classes.
When an object is initialized, by use of the constructor, and is of a myAL.Add(1);
reference type, the CLR must perform four operations: struct Person myAL.Add("foo");
(1) The CLR calculates the amount of memory required to hold the {
object on the heap. (2) The CLR inserts the data into the newly public string name; ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
public int height;
created memory space. (3) The CLR marks where the end of the
space lies, so that the next object can be placed there. (4) The CLR
} List (generic) (IEnumerable, code used inside a method)
Person dana = new Person(); dana.name = "Dana Dev";
returns a reference to the newly created space. List<string> list = new List<string>();
This occurs every single time an object is created. However the struct Person // with constructor list.Add("Bruce is cool");
assumption is that there is infinite memory, therefore some { list.Add("Yes he is");
maintenance needs to take place - and that's where the garbage string name; int height;
collector comes in. string s = list[1];
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ public Person(string name, int height) int len = list.Count;
{ List methods include: Sort, IndexOf, Insert, Reverse,
Nullable Types -- If x.HasValue is true, it is not null this.name = name; AddRange, RemoveRange, RemoveAt, etc
int? x = 10; x = null; int?[] arr = new int?[10]; this.height = height;
} ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
int y = x; //-- will not compile
int y = (int)x; //-- compiles, but throws exception if x null } //-- create Array, convert to List, convert back
int y = x.Value; //-- same as above string[] myArr = { "dog", "zebra", "ant", "parrot" };
public class StructWikiBookSample List<string> list = new List<string>(myArr);
►If you perform an operation with null, the result will be null {
public static void Main() string[] myArr2 = list.ToArray() as string[];
?? operator -- defines default assignment if target is null: {
//-- sort Array and List
Person dana = new Person("Dana Developer", 60) ;
int d = c ?? -1; // d = c, unless c is null, then d = -1 } Array.Sort(myArr);
int g = e ?? f ?? -1 // g = e, if e null, then f, or -1 } list.Sort();
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(Continued from 4. Collections) ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ (continued from Dictionary)
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Dictionary -- key/value pairs (keys are unique) //--- show key/value pairs
Generic classes (Better to use generics)
Generic class
Non-generic
Meaning ► A dictionary is a hash table where the details and foreach (KeyValuePair<string, string> kvp in dict)
counterpart
Collection<T> CollectionBase The basis for the collections complexity are hidden. Hash tables are faster than {
Comparer<T> Comparer
Compares two objects for search trees or other lookup structures. Why is this? WriteLine(kvp.Key + ", " + kvp.Value);
equality }
Dictionary<K,V> Hashtable
A collection of name-value Let's assume you want to fill up a library with books
pair and not just stuff them in there, but you want to be able
List<T>
A dynamic resizable list of //--- Create lists using dict keys or values
ArrayList
items to easily find them again when you need them. So, you
Queue<T> Queue FIFO list decide that if the person that wants to read a book var list1 = dict.Keys.ToList();
Stack<T> Stack LILO list
knows the title of the book, the person, with the aid of var list2 = dict.Values.ToList();
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ the librarian, should be able to find the book easily and foreach (string s in list1) { WriteLine (s); }
LinkedList -- allows constant time for insert or quickly. foreach (string s in list2) { WriteLine (s); }
remove, but only sequential access to elements list1.Sort(); //-- easy to sort
So, how can you do that? Well, obviously you can keep
LinkedList<string> LL = new LinkedList<string>(); some kind of list of where you put each book, but then //-- Sort using LINQ
LL.AddLast("aaa"); you have the same problem as searching the library, you var items = from x in dict orderby x.Value select x;
LL.AddFirst("bbb"); need to search the list. Granted, the list would be foreach(KeyValuePair<string, string> pair in items)
LL.AddLast("ccc"); smaller and easier to search, but still you don't want to { WriteLine (pair.Key + ", " + pair.Value); }
LL.AddLast("ddd"); search sequentially from one end of the library (or list)
to the other. //-- Sort using Lamda
LinkedListNode<string> node = LL.Find("ccc"); foreach (var pair in dict.OrderBy(p => p.Key))
LL.AddBefore(node, "hello there beautiful"); You want something that, with the title of the book, can { WriteLine(pair); }
Other methods include: AddAfter, Average, give you the right spot at once, so all you have to do is
Contains, Distinct, Find, FindLast, Max, Min, just stroll over to the right shelf, and pick up the book. //-- update value for a given key
OrderBy, Remove, ToList, etc So you devise a clever method: You take the title of the dict["cn"] = "People's Republic of China";
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ book, run it through a small computer program, which
spits out a shelf number and a slot number on that shelf. //-- xx
Queue -- First In, First Out (FIFO) This is where you place the book. x
Queue<string> q = new Queue<string>(); ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Dictionary<string, string> dict
q.Enqueue("aaa");
= new Dictionary<string, string>();
Don't bother with:
q.Enqueue("bbb"); (1) any of the non-generic collections or
q.Enqueue("ccc"); (2) SortedDictionary, SortedList, SortedSet or
WriteLine(q.Dequeue()); //-- removes and returns dict.Add("de", "Germany");
dict.Add("es", "Spain"); (3) Concurrent versions
WriteLine(q.Peek()); //-- returns, does not remove
dict.Add("uk", "Britain"); ► Some of these are similar to dictionary as they make
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ dict.Add("fr", "France"); use of key-value pairs, but are sorted or do other things
Stack -- Last In, First Out (LIFO) dict.Add("cn", "China"); a bit different.
Stack<string> stack = new Stack<string>(); WriteLine(dict["cn"]); //-- shows China ► The non-generic stuff should be considered
stack.Push("xxx"); WriteLine(dict["fr"]); //-- shows France deprecated. Don't use unless necessary for maint work
stack.Push("yyy"); on old code.
stack.Push("zzz"); ► Below are examples of what you can do to filter, sort ► Also--don't bother with Concurrent versions of these
WriteLine(stack.Pop()); //-- removes and returns and view dictionary keys and values. collections unless you are using multiple threads
WriteLine(stack.Peek()); //-- return, don't remove
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
5. Classes static vs. instantiated
► fields, methods and classes can be static
polymorphism
public class Animal
-- overriding what you Inherited

► Fields can be assigned in class declarations (compiler ► static class can't be instantiated or derived from {
will automatically move initialization to constructor) ► static class can contain only static methods and fields public virtual string talk() { return "Hi"; }
public string sing() { return "lalala"; }
► Below is class with constructors ► static is good enough if the class holds no state info }
► a static method can't access non-static class members
public class Employee public class Cat : Animal
{ Car ford = new Car(); // instantiating a non-static class {
public Employee() MyStaticClass.MyMethod(); // invoking method from static class public override string talk() { return "Meow!"; }
{ }
Console.WriteLine("Constructed without parameters"); ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
} encapsulation -- restrict access for safety -- public class Dog : Animal
{
public Employee(string strText) public / private -- private methods and fields can only be public override string talk() { return "Woof!"; }
{ accessed from other class methods, not from outside the class. public new string sing() { return "woofa woofa woooof"; }
Console.WriteLine(strText); Classes, methods and fields are private by default. }
}
}
Enums and structs are public by default. public class Polymorphism
protected -- like private except derived classes can access {
► Constructors can call each other: public Polymorphism()
internal -- restricted, only members of the class can access. {
public class Employee An internal class is not accessible from an external program. write(new Cat());
{ write(new Dog());
(This is only needed in larger, more complex programs)
public Employee(string text, int nbr) { ... } }
protected internal -- combination of protected OR internal public void write(Animal a)
// calls above with user text and default nbr {
public Employee(string text) : this(text, 1234) { ... } sealed -- class that cannot be derived from WriteLine(a.talk());
}
// calls above with default text ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ }
public Employee() : this("hello") { ... }
} inheritance (derived class B inherits from parent A) another example
public interface Animal
this is used to refer to member of the current instance of Class A { ... } {
class. It cannot refer to static field or method or be inside a string Name { get; }
Class B : A { ... } }
static class. It is often not required and may be infered by the
compiler, but it clarifies intent to human readers. example: public class Dog : Animal
base is used in constructors of a child class to refer to the {
public class BaseClass
instance of the parent class public string Name { get { return "Dog"; } }
{
}
public string HelloMessage = "Hello, World!";
class A {public string myStr = "hello"; } }
public class Cat : Animal
class B : A {
public class SubClass : BaseClass
{ public string Name { get { return "Cat"; } }
{
public string myStr = "world"; }
public string ArbitraryMessage = "Uh, Hi!";
public void MyWrite() }
{ Console.WriteLine( base.myStr + " " + this.myStr ) } public class Test
} { static void Main()
public class Test
{
{
public class B : A Animal animal = new Dog();
static void Main()
{ Animal animal2 = new Cat();
{
// constructor for class B (constructor for A executed first) Console.WriteLine(animal.Name);
SubClass subClass = new SubClass();
public B () : base () { } Console.WriteLine(animal2.Name);
Console.WriteLine(subClass.HelloMessage);
} }
}
}
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ }
(Continued from 5. Classes) ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ (continued from Extension Methods)
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ► Virtual methods are subject to override
public static class ExtensionMethods
get or set class properties using accessors {
class A
► The variable below is too easy to access from outside { public static string UppercaseFirstLetter(this string value)
the class, this lacks discipline, invites trouble. public virtual void Act() { } {
} if (value.Length > 0)
class Culture {
{ class B : A char[] array = value.ToCharArray();
public string Language; { array[0] = char.ToUpper(array[0]);
} return new string(array);
public override void Act()
}
{ Console.WriteLine("Perl.Act"); }
► Accessors below can be used as gatekeepers or return value;
}
}
modify read/write, maintain maximum control, etc.
}
► Abstract classes require a derived class, and cannot
class Culture be directly instantiated. They can contain abstract ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
{ methods, which require override methods. ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
public string Language { get; set; } abstract class A partial -- partial class can be split across more than one file
} {
public int ok;
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
-- or -- public abstract void MyMethod(); nested -- like it sounds, one class nested inside another
class Culture }
class A
{ {
private string lang; class B : A
{ public int v1;
public string Language
{ public override void MyMethod()
{ public class B { public v2; }
get { return "my " + lang; } Console.WriteLine("this is cool"); }
set { lang = value; } base.ok++;
} } class Program
} } {
static void Main()
{
protected set //-- permission only to derived classes interface -- Similar to abstract class. Contains A a = new A();
classes, methods and properties (but not fields). Widely a.v1++;
► As a principle, anything that you can keep from used (behind the scenes) to implement work with
allowing other code to touch, should be kept that way. A.B ab = new A.B();
IEnumerable and foreach. Don't worry about it. ab.v2++;
If other classes will need to view something in your class, you'll need
to expose a getter, but not a setter. They also allow you to perform ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ }
calculations or validations before returning or setting data. For }
example, if you have some integer value that should be within some
Extension methods -- Custom extension ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
range, in your setter you can check to make sure this condition is met methods are very cool ! You create a static class that uses the
before actually updating your value. this keyword, and it acts like an instance method for
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ whatever you want to extend. See example below.

Generally, you will want to store your extension method class


in a separate source file, such as "ExtensionMethods.cs" in
your project.
(continued next page)
string s = "dot net perls";
s = s.value.UppercaseFirstLetter();

(Continued next column)


■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
6. LINQ and Lambda -- 4 -- Using Lambda (need better example)
7. Misc Stuff
public struct Word_Freq { public string word; public int freq; }
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ List<Word_Freq> wfList = new List<Word_Freq>(); ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
► This section is not complete, or very good. If I // .. add to and update the wfList ... Anonymous Methods
revise these notes--this section will be my priority. wfList.Sort( (x, y) => -- define functionality of a delegate (such as an event) inline
((1000 - x.freq).ToString() + x.word).CompareTo( rather than as a separate method.
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ((1000 - y.freq).ToString() + y.word))
See: http://csharp-station.com/Tutorial/CSharp/Lesson21
LINQ ---- examples ---- );
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
-- 1 -- Some Lambda notes: (need something better here) Anonymous Types -- provide a convenient way to
List<int> elements = new List<int>() { 10, 20, 31, 40 }; encapsulate a set of read-only properties into a single object
// ... Find index of first odd element. without having to explicitly define a type first:
int oddIndex = elements.FindIndex(x => x % 2 != 0); A lambda expression is an anonymous function and it is
mostly used to create delegates in LINQ. Simply put,
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ var v = new { Amount = 108, Message = "Hello" };
it's a method without a declaration (i.e., access Console.WriteLine(v.Amount + v.Message);
-- 2 --
string[] a = new string[] { "US", "China", "Peru", "UK", "Spain" }; modifier, return value declaration, and name).
(Behind the scenes, they are actually classes of type object)
var sort = from s in a orderby s select s; Convenience. It's a shorthand that allows you to write a
foreach (string c in sort) { Console.WriteLine(c); } method in the same place you are going to use it. You can return an anonymous object from your function
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ public static object FunctionWithUnknowReturnValues ()
//-- Lambda with no inputs: ()=>
-- Cars example -- {
delegate int del(int i); /// anonymous object
List<Car> cars=new List<Car>() return new { a = 1, b = 2 };
{ static void Main(string[] args) }
{new Car() {id=100, color="blue", speed=20, make="Honda"}}, { dynamic x = FunctionWithUnknowReturnValues();
{new Car() {id=101, color="red", speed=30, make="Ford"}}, del del_1 = y => y * y; Console.WriteLine(x.a);
{new Car() {id=102, color="green", speed=40, make="Honda"}}, int j = del_1(5); Console.WriteLine(x.b);
{new Car() {id=103, color="blue", speed=40, make="Ford"}}, WriteLine(j);
... ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
{new Car() {id=111, color="green", speed=10, make="Ford"}}
};
del del_2 = x => x + 2;
j = del_2(4);
String functions
WriteLine(j); ► examples: Length, Substring, IndexOf, ToLower,
var subset= from rows in cars }
where rows.make != "Toyota" ToUpper, Insert, Split, Replace, Remove, Trim, etc
&& rows.speed > 10 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
orderby rows.color, rows.speed descending To sort chars in string
select rows; More LINQ examples here: static string SortMyString(string s)
(1) (MSDN) LINQ query examples (a good start) {
List<Car> cars2=new List<Car>(); (includes joins and all kinds of things) char[] myChars = s.ToCharArray();
foreach (Car car in subset) { cars2.Add(car); } https://msdn.microsoft.com/en-us/library/gg509017.aspx Array.Sort(myChars);
return new string(myChars);
(2) http://studyoverflow.com/linq/ }
public class Car
{ (3) 50 LINQ Examples, Tips and How To's
public int id; http://www.dotnetcurry.com/linq/727/linq-examples-tips-tricks To replace a section of a string
public string color;
(4) 101 LINQ SAMPLES string s = "One step at a time, Bruce will make this easy";
public int speed;
https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b //-- method # 1
public string make;
} (5) LINQ 101 Samples - Lambda Style int spot = s.IndexOf("Bruce");
http://linq101.nilzorblog.com/linq101-lambda.php int sLen = "Bruce".Length;
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ string section1 = s.Substring(0, spot);
(6) Wikipedia string section2 = s.Substring(spot + sLen);
Lots of info about LINQ to XML, SQL and data sets string s2 = section1 + "Heather" + section2;
https://en.wikipedia.org/wiki/Language_Integrated_Query
//-- method # 2
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ string s3 = s.Replace("Bruce", "Heather");
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Formated String -- example IEnumerable and yield -- Indicates that method (or get unsafe {} // for use with pointers
accessor) that contains this is an iterator, and that state needs to be ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
stored. Iterator is consumed with foreach or a LINQ query. Return
string v1 = "Dot Net Perls"; type must be IEnumerable or IEnumerator (with or without <T>). Constraints -- (on type parameters)
int v2 = 10000;
DateTime v3 = new DateTime(2016, 8, 16); public class PowersOf2 public class GenericList<T> where T : Employee
string result1= string.Format("{0}, {1}, {2}", v1, v2, v3); {
string result2= string.Format static void Main() The constraint enables the generic class to use the
("{0}: {1:0.0}, {2:yyyy-MM-dd}", v1, v2, v3); { Employee.Name property because all items of type T are
Console.WriteLine(result1); // Display powers of 2 up to the exponent of 8: guaranteed to be either an Employee object or an object that
Console.WriteLine(result2); // Output will be: 2 4 8 16 32 64 128 256 inherits from Employee.
foreach (int i in Power(2, 8))
Result: { Console.Write("{0} ", i); } ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Dot Net Perls, 10000, 8/16/2016 12:00:00 AM } Delegate -- similar to pointer to function in C or C++. It is
Dot Net Perls: 10000.0, 2016-08-16 a ref type variable pointing to a method, and can be changed
public static IEnumerable<int> Power(int nbr, int exponent) at runtime. Used in such things as binding events to event
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ {
String split example handlers, like if you click a button. Or, more generally, when
int result = 1;
you want to send a method as a parameter to another method.
for (int i = 0; i < exponent; i++)
string[] words {
= text.Split(' ', '.', ',', '-'); result = result * nbr; example: myList.Foreach( i => i.DoSomething());
yield return result;
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ } see: "C# delegates and equivalent Java constructs" here:
StringBuilder -- A more mutable form of string, eliminates } https://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java
auto creation, copy and reassign each time you modify it. } ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Another example (using nested classes) Tuples -- are cool
File I/O (reading and writing text file) public static class GalaxyClass var tuple = new Tuple<string, int, bool, MyClass>
{
public static void ShowGalaxies()
("foo", 123, true, new MyClass());
//-- true in statement below is the flag for Append {
using (StreamWriter sw var theGalaxies = new Galaxies(); List<Tuple<int, string>> list = new List<Tuple<int, string>>();
foreach (Galaxy theGalaxy in theGalaxies.NextGalaxy)
= new StreamWriter(filePathAndName, true)) {
list.Add(new Tuple<int, string>(2, "foo"));
{ WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears.ToString()); list.Add(new Tuple<int, string>(1, "bar"));
foreach (string s in myText) { sw.WriteLine(s); } } list.Add(new Tuple<int, string>(3, "qux"));
} } // ShowGalaxies

//sort based on the string element


public class Galaxies
List<string> myText = new List<string>(); { list.Sort((a, b) => a.Item2.CompareTo(b.Item2));
using (StreamReader sr = new public IEnumerable<Galaxy> NextGalaxy
StreamReader(filePathAndName)) { foreach (var element in list)
get
{ { {
string line; yield return new Galaxy { Name = "Tadpole", MegaLightYears = 400 }; Console.WriteLine(element);
while ((line = sr.ReadLine()) != null) yield return new Galaxy { Name = "Pinwheel", MegaLightYears = 25 }; }
yield return new Galaxy { Name = "Milky Way", MegaLightYears = 0 };
{ yield return new Galaxy { Name = "Andromeda", MegaLightYears = 3 };
myText.Add(line); } // Output:
} } // Next galaxy // (1, bar)
} // Galaxies
} // (2, foo)
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ // (3, qux)
public class Galaxy
{
public String Name { get; set; }
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
public int MegaLightYears { get; set; } P/Invoke -- Platform Invoke used on .Net Compact
} Framework is used to invoke functions in external DLL's.
} // GalaxyClass

■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

--[]--

You might also like