C# basics

1) Single inheritance means that a C# class can inherit from only one base class.

2) C# enables you to group your classes into a collection of classes called a namespace.
Namespaces have names, and can help organize collections of classes into logical groupings.

3) C# lets you work with two types of data: value types and reference types. Value types hold
actual values. Reference types hold references to values stored elsewhere in memory.

4) Primitive types such as char, int and float, as well as enumerated values and structures, are
value types. Reference types hold variables that deal with objects and arrays

5) You can work with both one-dimensional and multidimensional arrays in C#.
Multidimensional arrays can be rectangular, in which each of the arrays has the same
dimensions, or jagged, in which each of the arrays has different dimensions.

6) Functions can have four kinds of parameters:

• Input parameters have values that are sent into the function, but the function cannot
change those values.
• Output parameters have no value when they are sent into the function, but the function
can give them a value and send the value back to the caller.
• Reference parameters pass in a reference to another value. They have a value coming
in to the function, and that value can be changed inside the function.
• Params parameters define a variable number of arguments in a list.


7) Enum type declarations specify a type name
for a related group of constants, Using the enum names in code makes code more readable

8) C# provides a built-in mechanism for defining and handling events. If you write a class that
performs a lengthy operation, you may want to invoke an event when the operation is
completed. Clients can subscribe to that event and catch the event in their code, which enables
them to be notified when you have completed your lengthy operation. The event handling
mechanism in C# uses delegates, which are variables that reference a function.

9) You can write a piece of code called an indexer to enable your class to be accessed
as if it were an array. Suppose you write a class called Rainbow, for example, that contains a
set of the colors in the rainbow. Callers may want to write MyRainbow[0] to retrieve the first
color in the rainbow. You can write an indexer into your Rainbow class to define what should
be returned when the caller accesses your class, as if it were an array of values.

10) Attributes declare additional information about your class to the CLR. Attributes
enables you, the developer, to bind information to classes. Attributes can also be used to bind runtime information to a class

11) The code that is output by the C# compiler is written in a language called Microsoft
Intermediate Language, or MSIL

12) All .NET-compatible languages, including Visual Basic .NET and Managed C++,
produce MSIL when their source code is compiled. Because all of the .NET languages
compile to the same MSIL instruction set,

13) MSIL code is turned into
CPU-specific code when the code is run for the first time. This process is called "just-in-time"
compilation, or JIT. The job of a JIT compiler is to translate your generic MSIL code into
machine code that can be executed by your CPU.

14) The compilation process also outputs metadata, Think of metadata as a "table of contents" for your compiled code. The C# compiler places
metadata in the compiled code along with the generated MSIL. This metadata accurately
describes all the classes you wrote and how they are structured.


15) An assembly is a package of code and metadata. When you deploy a set of classes in an
assembly, you are deploying the classes as a unit; and those classes share the same level of
version control, security information, and activation requirements. Think of an assembly as a
"logical DLL."

16) There are two types of assemblies: private assemblies and global assemblies. When you build
your assembly, you don't need to specify whether you want to build a private or a global
assembly. The difference is apparent when you deploy your assembly. With a private
assembly, you make your code available to a single application.

17) The .NET Framework contains a list of global assemblies in a facility
called the global assembly cache, and the .NET Microsoft Framework SDK includes utilities
to both install and remove assemblies from the global assembly cache. GAC


18) Every application in C# must have a method called Main().

19) public static void Main()

By declaring your Main() method as public, you are
creating an entry point for Windows to start the application when a user wants to run it.

The word Static in the method declaration means that the compiler should allow only one
copy of the method to exist in memory at any given time. Because the Main() method is the
entry point into your application, it would be catastrophic to allow the entry point to be loaded
more than once

Void means that your application returns no value after it has
completed. This sample application isn't very advanced, so no return value is needed; under
normal circumstances, however, the Main() function would typically return an integer value
by replacing the word void with int.

20) The C# compiler does not ordinarily allow you to use any of the reserved keywords as an
identifier name. You'll get an error if, for example, you try to name a class static. If you really
need to use a keyword name as an identifier, however, you can precede the identifier with the
@ symbol. This overrides the compiler error and enables you to use a keyword as an
identifier.

21) The C# compiler does, in fact, accept any
of four possible constructs for the Main() function:
• public static void Main()
• public static void Main(string[] Arguments)
• public static int Main()
• public static int Main(string [] Arguments)

The second form, public static void Main(string[] Arguments), does not return a value to the
caller. It does, however, take in an array of strings. Each string in the array corresponds to a
command-line argument supplied when the program executes.

22) C# makes a distinction between assigned and unassigned variables. Assigned variables are
given a value at some point in the code, and unassigned variables are not given a value in the
code. Working with unassigned variables is forbidden in C#, because their values are not
known and using the variables can lead to errors in your code. HOWEVER In some cases, C# gives default values to variables. A variable declared at the class level is
one such case.

23) C# enables you to group variables into structures. By defining a structure for your data, the
entire group can be managed under a single structure name, regardless of the number of
variables that the structure contains. The entire set of variables can be easily manipulated by
working with a single structure, rather than having to keep track of each individual variable
separately

24) Structures in C# are value types, not reference types. This means that structure variables
contain the structure's values directly, rather than maintaining a reference to a structure found
elsewhere in memory.

25) Structures may also be made up of variables of different types.

struct Employee
{
public string FirstName;
public string LastName;
public string Address;
public string City;
public string State;
public ushort ZIPCode;
public decimal Salary;
}

As with all statements in C#, you can declare a structure only from within a class.
you can then use it like:

Employee emp;
emp.Firstname

26) C# does not allow you to initialize structure
members when they are declared. Take a look at the error in the following code:
struct Point
{
public int X = 100;
public int Y = 200;
}
This declaration produces errors from the compiler:
error CS0573: 'MyClass.Point.X': cannot have instance field
initializers in structs
error CS0573: 'MyClass.Point.Y': cannot have instance field
initializers in structs

You can use a special method called a constructor to initialize structure members to nonzero
values.


27) If you want to invoke a constructor on a structure, you must use the new keyword, followed by the
name of the structure,

Point MyThirdPoint = new Point(250, 475);

This statement says: "Create a new Point structure using the constructor with two integers.
Assign its value to the MyThirdPoint variable.

28) The parameterless constructor syntax is also shown in Listing 7-3:

Point MyFirstPoint = new Point();

This tells the C# compiler that you want to initialize the structure using its default behavior.

29) You may also write methods in your structures. These methods follow the same rules as class
methods:

30) Properties within a structure enable you to read, write, and compute values with the use of
accessors. Unlike fields, properties are not considered variables; therefore, they do not
designate storage space. Because a property does not allocate storage space, it cannot be
passed as a ref or out parameter.

31) Interfaces are a way of ensuring that someone using your class has abided by all the rules you
set forth to do so. This can include implementing certain methods, properties, and events.
When you expose an interface, users of your interface must inherit that interface; and in doing
so, they are bound to create certain methods and so forth. This ensures that your class and/or
structure is used as it was intended.
You can include an interface within a structure as well.

No comments:

Post a Comment