C# Language Basics Imran Rashid CTO at ManiWeber Technologies
Outline The Nature of C# A Class Definition The Base Class Library (BCL) Diagramming C# Classes Guidelines
The Nature of C#
The Nature of C# C# is an object-oriented language not procedural Does not supports global variables or functions Objects are defined in classes and structures Persistent data is stored in member fields Behaviors are defined in member methods Everything is part of a class or structure So in C# everything is an object Members are also called fields, attributes or properties Methods are also called functions, actions or behaviors Objects are also called instances
A Class Definition
A Class Definition
The Base Class Library (BCL)
The Base Class Library (BCL) Defined classes and for all frameworks File management, math, other common operations Example: Mathematical Operations Common math tasks are in System.Math System is the name of Namespace Math is the name of Class Most operations are static methods You can access a static member using the type name instead of a reference or a value e.g. Math.Round(double) using System.Math; double pi = Math.PI; // A field double rounded = Math.Round(pi, 2); // A method Console.WriteLine(rounded); // 3.14
Diagramming C# Classes
Diagramming C# Classes
Guidelines
Guidelines Who Decides? Naming Rules Enforced by Compiler Some naming rules are enforced by the complier Unforced guidelines are suggested by Microsoft Some controversy / disagreements do exists Solo developers get to decide for themselves Teams should agree on guidelines for their projects Naming Rules Enforced by Compiler Identifiers names can include alphanumeric characters Cannot include special characters other than underscore Must start with an alpha or underscore, not a number
Guidelines Case Sensitivity firstName FirstName FIRSTNAME An identifier is the name of a programming element What’s named: methods, variables, properties, and more Identifier in C# are case-sensitive; these are different firstName FirstName FIRSTNAME
Guidelines General Recommendations Variables, Fields, Parameters Choose easily readable identifiers names Always favor readability over simplicity or shortcuts No underscores, hyphens, other special characters No “Hungarian” notation: strFirstName, iAge Exception: naming visual objects in XAML Variables, Fields, Parameters Start with lower-case character Use camel-case to distinguish words int i = 0; double thePrice = 14.95; string firstName = “Gims”;
const int HomeRunRecord = 61; Guidelines All Others: Pascal Casing Initial upper-case character, camel-case for the rest Use camel-case only notation for variables and parameters Use Pascal casing for all other identifiers: Methods Constants Properties const int HomeRunRecord = 61;
Any ?