Download presentation
1
Introduction to C# With a GIS focus
With Python examples to help with transition Provide a C# foundation for ArcGIS customization with ArcObjects
2
Why customize ArcGIS ? Workflow.
ArcGIS application like ArcMap does not do EXACTLY what you want to do “out-of-the-box”. ArcObjects provides the programming framework customize ArGIS applications to do exactly what you want.
3
Extending ArcGIS Add-ins, C#, & ArcObjects
Adding toolbars, buttons, tools, etc. ArcGIS provides tools for making Python Add-ins ArcGIS 10.0 did not (Free webinar) ArcObjects SDK for the Microsoft .NET Framework provides access to more than Python
4
Customization example
Capture point features at midpoint of drawn line automatically calculating and storing line orientation and providing easy tools for selecting type and adding dip angle.
5
ESRI Customization Examples
200+ ESRI examples in Samples folder i.e. C:\Program Files\ArcGIS\DeveloperKit10.2\Samples\ArcObjectsNet
6
From Python to C# IDE Solutions, Projects Runtime environment
Code containers Core language and framework Types Variables, operators, expressions, statements Functions Scope Booleans Flow control – branching and looping Standard modules and libraries
7
IDE Projects PyScripter psproj Visual Studio sln and csproj
One or more related files referenced by psproj file Solution (sln) can contain one or more Projects (csproj) and other files Project contains one or more related files Project is optional Solution/Project(s) not optional
8
Visual Studio IDE Creating .NET Solution and Projects
with Visual Studio 2010
9
Projects … … because any application worth building usually has more than one .cs file and other files (e.g. images, cursors, etc.)
10
Visual Studio Solution (.sln)
… because Projects must be part of a Solution Visual Studio Solution (.sln) * Projects (.csproj)
11
Folder structure for application development
Download …
12
Creating a src folder - Create a New Project … Blank Solution, in src - Save - Exit Visual Studio - Move files up one level into src - Delete solution folder Solutions can include code from other CLS languages (e.g. VB.NET)
13
Adding a Project to a Solution
14
Project templates EXE EXE DLL ESRI Add-ins and Extensions
are compiled into DLL’s Most common Project templates
15
Sln and Project code
16
StartUp Project StartUp Project is the first project to run when the application is run Class Library Projects cannot be set as the StartUp Project
17
Runtime requirements Python C#
PY file run from command line or double click Can be run interactively >>> x = 1 Requires Python interpreter (Python.exe) to run Requires appropriate version of Python.exe EXE run from command line or double click Cannot be run interactively No >>> here. Compiled assembly (EXE) can be run directly Requires .NET Framework that can support assembly
18
Runtime – from code to CPU
Python C# Python file run at command line or double-clicked Interpreted by Python.exe into byte code Byte code transformed into machine language by Python runtime CPU runs machine language program Code files in Visual Studio Solution/Project(s) compiled to Common Intermediate Language (CIL) Assembly (EXE) EXE run from command line or double-clicked Common Language Runtime (CLR) transforms CIL into machine language CLR and CPU run program
19
.NET Framework Overview
CLS specifes rules .NET languages must follow to create .NET code that will reference the .NET Framework Class Libraries (.NET DLL Assemblies) and .NET CTS. CLS (Common Language Specification) C#, VB.NET, Managed C++, J# Framework Class Libraries UI Web Form WinForm WPF Services WCF WorkFlow Data Access ADO.NET Entity Framwork LINQ to SQL Framework Base Classes & CTS (Common Type System) IO, Collections, Security, Threading, etc. .NET compilers will compile .NET code into CIL stored in .NET assemblies (EXE or DLL) Compilers CIL (Common Intermediate Language) .NET CLR will perform JIT (Just-In-Time compilation) to native machine code and ensure that the .NET assembly runs properly and securely. CLR (Common Language Runtime) Windows OS Win32, IIS, MSMQ
20
Statements Code is interpreted or compiled one statement at a time
A statement can be composed of one or more expressions Expressions are composed of one or more of the following: variables, operators, literals, function calls, and types
21
Statements Python C# No end of statement marker
Indentation is critical msg = “Multi-line statement \ needs line continuation char” ; marks end of statement Indentation is cosmetic msg = “Multi-line statement does not need continuation char”;
22
Statement context Python C# Statements can be run outside of functions
Statements cannot be run outside of functions Solution Project Namespace Class Method 1 keyword 1 literal 4 keywords 4 names 1 literal 1 4 Statement
23
Keywords Python C# ~30 ~80
24
Types Python C# str int float bool (True or False) string, char
sbyte, byte, short, ushort, int, uint, long, ulong float, double, decimal bool (true or false)
25
C# Types – Value types 7 significant digits, ±3.4 x 1038 true, false
0 – 255 ‘a’, ‘b’, ‘c’, ‘\t’, etc. 28-29 significant digits, ± 7.9 x 1028 15-16 significant digits, ± 5.0 x to ±1.7 x 10308 List of constants of same type - byte, sbyte, short, ushort, int, uint, long, or ulong 7 significant digits, ±3.4 x 1038 -2,147,483,648 to 2,147,483,647 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 -128 to 127 -32,768 to 32,767 Group of related value types 0 to 4,294,967,295 0 to 18,446,744,073,709,551,615 0 to 65,535
26
C# Types – Reference types
Value types require a static amount of memory (e.g. byte requires 1 byte, int uses 4 bytes, etc.) and directly contain their data. Reference types require dynamic amounts of memory (e.g. “Bob” and “Bobby”). Stack memory Stores values of value types and memory location of reference types in Heap Memory Heap memory Stores reference types
27
Most commonly used types
string int double bool char
28
Classes Python C# Syntax: Syntax: class name([baseClass]):
[properties (data)] [methods] Syntax: [scope][static] class name : baseClass { [properties] [methods] [events] etc. }
29
Variables Python C# Variable is case-sensitive name associated with a value of any type Type defined after assignment Variable is case-sensitive name associated with a value of a specific type Type defined in variable declaration statement
30
Variable declaration/initialization
<type> <variableName> {= initialValue}; int x; // Declare variable x = 3; // Initialize variable int x = 3; // Declare and initialize int x, y, z; // Not recommended // Declare on separate lines
31
Rules for variable naming
Same for Python and C# Variable name syntax: _ or letter + any number of letters, digits, or _ i.e. cannot begin with a number Use names that describe the data they are associated with e.g. “recordCount” is better than “n” Use lowercase letter for first character, upper case letter for other words in name. This is called camelCase. Cannot be a keyword of the language
32
Operators Python C# +, -, *, /, % same as C# 2**3 is 23
+, -, *, /, % same as Python Math.Pow(2,3) Output: 0.5
33
C# Operators and Expressions
<variable> = <operand> <operator> <operand> <variable> <operator> <operand> <variable> <operator> // x = x + 1, z = x + 1 // x = x + 1, z = x // x = x - 1, z = x – 1 // x = x - 1, z = x ++x add before expression evaluated x++ add after expression evaluated p. 46, 47 in Watson et al.
34
Operator Precedence Same for Python and C# Parentheses/Brackets
Exponentiation Muliplication – Division Addition - Subtraction Output: 17
35
Operator polymorphism
Python C# + addition + string concatenation * multiplication * string repetition + addition + string concatenation
36
Comments Python C# // Comment out single line /* Comment out multiple lines */ comments with //
37
C# Comments Examples //, ///, /* */
//, ///, /* */ “The best kind of comments are the ones you don't need. Allow me to clarify that point. You should first strive to make your code as simple as possible to understand without relying on comments as a crutch. Only at the point where the code cannot be made easier to understand should you begin to add comments.” From
38
Functions – Syntax for Definition
Python C# Syntax: def name([params]): statements [return value] Syntax: [scope][static] type name ([params]) { statements [return [value];] } scope keywords include public, internal (default), protected, protected internal, private If static is included, function belongs to class instead of instance of class type refers to type returned by function Can be any type including user-defined types. Is “void” if it returns nothing params include type (e.g. string param1)
39
Functions – Define and call
Syntax: [scope][static] type name ([params]) { statements [return [value];] } Define: Call:
40
Scope “Visibility” of variables, functions, classes, etc in a program.
[scope][static] class name Function: [scope][static] type name ([params]) Variables: [scope][static] type name [= value];
41
Scope “Visibility” of variables, functions, classes, etc in a program.
Python C#
42
Scope Review public internal (default) protected Entire solution
private More … Assembly only More … Derived classes More … Class only More … 42 of 29
43
Booleans Python C# True value: true False value: false
44
Boolean Operators Python C# or, and |, ||, &, &&
Comparison operators are the same for Python and C#
45
Flow control - Branching
46
Flow control - Branching
Python C# if condition: # Statements for true # condition elif condition: # Statements for alternate # true condition else: # if all above conditions # are false if (condition) { // Statements for true // condition } else if (condition) // Statements for alternate // true condition else // if all above conditions // are false
47
Branching with if
48
Branching with switch
49
Replacing simple if … else
If the “if … else” is being used to simply set a boolean variable, set the variable using a boolean assignment statement Replace this with or
50
Ternary (conditional) Operator ?
Used to set a non-bool type variable based on a condition Syntax: variable = condition ? true_expression : false_expression Replace this with or
51
Flow control - Repetition
Previous code Code to repeat Is condition true? Next code Loop
52
Flow control - Repetition
Python C# for target in list: # Statements executed for # each item in list. # Number of iterations # controlled by number of # values in list # target is assigned each # value in list foreach (type target in list) { // Statements executed for // each item in list. } for (type target = first_value; condition_to_continue; change_target_variable) // Statements executed // while condition to // continue is true
53
Looping with for
54
Looping with while
55
Looping with do
56
Interrupting loops continue break return
Continue with next target value break Exit from loop return Exit from loop & containing function
57
Standard modules/libraries
Python C# Standard modules sys os os.path urllib xml shutil glob etc. Framework Class Libraries (DLL) organized into Namespaces like System System.Collections System.IO System.Web System.XML System.Data etc.
58
Using modules/libraries
Python C# C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll
59
using keyword System namespace is part of
mscorlib (Microsoft Common Object Runtime Library) All C# programs require a reference to mscorlib
60
Class libraries, namespaces, classes
Class Libraries are stored in a .NET assembly (DLL) Namespaces are a way of naming a group of related classes and other types Namespaces can be nested. Namespace icon
61
.NET Framework Overview
CLS specifes rules .NET languages must follow to create .NET code that will reference the .NET Framework Class Libraries (.NET DLL Assemblies) and .NET CTS. CLS (Common Language Specification) C#, VB.NET, Managed C++, J# Framework Class Libraries UI Web Form WinForm WPF Services WCF WorkFlow Data Access ADO.NET Entity Framwork LINQ to SQL Framework Base Classes & CTS (Common Type System) IO, Collections, Security, Threading, etc. .NET compilers will compile .NET code into CIL stored in .NET assemblies (EXE or DLL) Compilers CIL (Common Intermediate Language) .NET CLR will perform JIT (Just-In-Time compilation) to native machine code and ensure that the .NET assembly runs properly and securely. CLR (Common Language Runtime) Windows OS Win32, IIS, MSMQ
62
Keys to remember F1 – Context sensitive Help (If cursor is on keyword, help on keyword provided) F6 – Build code (compile) without running F5 – Build code (compile) and run F11 – Step through code, into called functions F10 – Step through code, not into called functions Shift-F5 – Stop running code F9 – Toggle Breakpoint at current statement
63
Links for VS 2010 and C# “Quick Tour” of the VS 2010 IDE
Beginner Developer Learning Centre Csharp-station.com tutorial
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.