Вы находитесь на странице: 1из 2

Chapter-2 Basic Coding in C# C# requires a certain amount of structure: code is made up of statements that live inside a Method, which

belongs to a type which is typically inside a namespace, all inside a file that is part of a Visual Studio Project. Only Code Console.WriteLine(Hello World); Whole Code Using System; Namespace Hello; { class Program { Static void Main() { Console.WriteLine(Hello World); }}}

Variables(Local) C# lets you define Local Variables, which are named elements inside a method that each hold a piece of information. In C# Specification, the term variable can refer to local variables, but also to fields in array elements & objects. C# is statically-typed language which is to say that any element of code that represents or produces information, such as a variable, or an expression, has its data type determined at compile time. This is different than dynamically-typed language such as Javascript, in which types are determined at runtime. Beginning of a Variable Variable must begin with either letter or underscore, which Can be followed by any Combination of the characters String part1=The ultimate Question; Described in the Identifier & Pattern Syntax annex of the String part2=of something; Unicode Specification. If you are using Text in ASCII range, Int theAnswer=42; That means letters, decimal digits & underscores. If you are Int something; Using Unicodes full range, this also includes various Accents, diacritics & numerous somewhat obscure punctuation marks. These same rules determine what constitutes a legal identifier for any user defined entity in C#, such as a class or method.
Assigning Values to previously declared variables

C# Typing (Variable Declaration)

Part2=of life, the universe, and everything; Something=123; Because variables have a static type, the compiler will reject attempts to assign a text string into it. For eg. theAnswer=The compiler will reject this;

You dont have to state the variable type explicitly. You can let the compiler work it Out for you by using var. This example shows the first 3 variable Declarations from previous examples, but using var instead of Explicit Implicit variable types with the Data types. This code often misleads people who know javascript, var keyword Because that also has a var keyword that you can use in a var part1 = "the ultimate question"; Similar-looking way. But var does not work the same way in var part2 = "of something"; var theAnswer = 42;

C# as in Javascript:these variables are still all statically typed. All thats changed is that we havent said what the type iswere letting the compiler deduce it for us. It looks at the initializers, and can see that the first two variables are strings while the third is an integer. (Thats why I left out the fourth variable from Example of something. That doesnt have an initializer, so the compiler would have no way of inferring its type. If you try to use the var keyword without an initializer, youll get a compiler error.) were trying to assign a text string into a variable of an incompatible type. That variable, theAnswer, has a type of int here, even though we didnt say so explicitly.
An error: the wrong type (again)

var theAnswer = 42; theAnswer = "The compiler will reject this";

Declaring Multiple Variables

You can declare, and optionally initialize, multiple variables in a single line. If you want multiple variables of the same type, this may reduce clutter in your code. This example declares three variables of the same type in a single declaration. double a = 1, b = 2.5, c = -3;

Using and Concatenating (+) Variables


Code Output

string part1 = "the ultimate question"; string part2 = "of something"; int theAnswer = 42; part2 = "of life, the universe, and everything"; string questionText = "What is the answer to " + part1 + ", " + part2 + "?"; string answerText = "The answer to " + part1 + ", " + part2 + ", is: " + theAnswer; Console.WriteLine(questionText); Console.WriteLine(answerText);

What is the answer to the ultimate question, of life, the universe, and everything?
-The answer to the ultimate question, of life, the universe, and everything, is: 42

Вам также может понравиться