Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to C# for Java people

Similar presentations


Presentation on theme: "Intro to C# for Java people"— Presentation transcript:

1 Intro to C# for Java people

2 C# Compiled (fast) Managed Code (no memory allocation) Strongly-typed
Object-oriented

3 Variable types float z = 3.0f; // same as Java bool x = 3 <5; string s = “word”; // strings are arrays char letter = s[0]; string[] sArray = new string[10]; // same string[] sArray2 = {“A”, “B”, “C”}; // same string[] sArray3; sArray3 = new string[] {“D”, “E”, “F”}; // different foreach(string s in sArray3){ // Lots of array methods!! Check the API…

4 2D arrays string[,] table = new string[4,8]; // commas!! int guess = table.Length; int guess2 = table.Rank; int guess3 = table.GetLength(0); foreach (string s in table){

5 Jagged Arrays // array of arrays is different, kinda string[][] table2 = new string[4][]; int x = table2.Length; int y = table2[0].Length;

6 Lists List<string> list = new List<string>(); list.Add(“First”); list.Add(“Last”); foreach(string s in list){ … string guess1 = list[0]; int guess2 = list.Count; Check API for more methods

7 A Rose by any other name…
using import namespace package : extends implements fields instance variables

8 Properties: Abstract access to private variables
public class Animal { private string species; public string Species { get { return species; } set { species = value; } }

9 Properties public class Animal { private string species;
public string Species { get { return species; } set { species = value; } } Animal animal = new Animal(); animal.Species = "Lion"; // set accessor System.Console.WriteLine(animal.Species); // get accessor

10 Properties You don’t have to have BOTH a set and a get
Auto-implemented Properties: public string Name { get; set; } public string FirstName { get; set; } = “Shannon”;

11 Be careful with properties!
Vector3 pos = transform.position; pos.x += speed; transform.position = pos; Is the same as?? transform.position.x += speed;

12 Nope…Position is a property...
transform.position.x += speed; Same as: transform.getPosition().x += speed; No setter called, so only local copy changes!

13 Naming conventions Methods are capitalized
Variable names are lower-case, Classes capitalized (same as Java) Property names are upper-case, usually… Private instance vars often start with underscore

14 Journey into 335 with Parameter Passing
How does Java pass parameters? Is there any way to alter the default scheme?

15 C# keyword ref: Pass by reference
class RefExample { static void Method(ref int i) { i = i + 44; } static void Main() { int val = 1; // Needs val to pass Method(ref val); // Pass val by reference Console.WriteLine(val); // Output: 45

16 C# Keyword out: doesn’t need initialization
class OutExample { static void Method(out int i) { i = 44; // MUST be given a value } // by CALLEE static void Main() { int val; Method(out val); Console.WriteLine(val); // Output: 44 }


Download ppt "Intro to C# for Java people"

Similar presentations


Ads by Google